zoukankan      html  css  js  c++  java
  • leetcode 中等难度

    L494

    //方法一:回溯法
    class Solution {
        public static int findTargetSumWays(int[] nums, int target) {
            cnt =0;
            recurve(nums, 0, 0, target);
            return cnt;
        }
    
        static int cnt = 0;
    
        public static void recurve(int[] nums, int n, int sum, int target) {
            if (n == nums.length) {
                if (sum == target) cnt++;
                return;
            }
            recurve(nums, n + 1, sum + nums[n], target);
            recurve(nums, n + 1, sum - nums[n], target);
        }
    }
    //方法二:深度优先搜索
    

    406. 根据身高重建队列

    方法一:堆排序
    public static int[][] reconstructQueue(int[][] people) {
            PriorityQueue<Integer[]> heap = sort(people);
            LinkedList<Integer[]> result = new LinkedList<>();
            //插入排序
            for (int i = 0; i < people.length; i++) {
                Integer[] x = heap.poll();
                result.add(x[1], x);
            }
            int[][] res = new int[people.length][2];
            for (int i = 0; i < people.length; i++) {
                Integer[] t = result.get(i);
                res[i][0] = t[0];
                res[i][1] = t[1];
            }
            return res;
        }
    
    
        public static PriorityQueue<Integer[]> sort(int[][] people) {
            PriorityQueue<Integer[]> heap = new PriorityQueue<>(new Comparator<Integer[]>() {
                @Override
                public int compare(Integer[] o1, Integer[] o2) {
                    if (o1 == null || o2 == null) return 0;
                    if (o1[0].equals(o2[0])) {
                        return o1[1] - o2[1];
                    }
                    return o2[0] - o1[0];
                }
            });
            for (int i = 0; i < people.length; i++) {
                heap.add(new Integer[]{people[i][0], people[i][1]});
            }
            return heap;
        }
    方法二:
    
    

    leetcode 完全平方数

    //方法1:
    

    I'm a fucKing fake coder!

  • 相关阅读:
    Performance and Design
    返回数组中不重复的元素
    IE的button元素bug
    (转)Google Closure: 糟糕的JavaScript
    Why do we have an IMG element?
    About this and that
    C#中的Attribute
    C#检查字体是否存储,以及安装
    ZipFile压缩文件后,解压文件后有多层目录的处理方法
    Office系列在线预览
  • 原文地址:https://www.cnblogs.com/zhouyu0-0/p/15213242.html
Copyright © 2011-2022 走看看