• Leetcode 递增子序列 回溯去重


      回溯遍历去重题。

      JAVA:

        List<List<Integer>> reList = new LinkedList<List<Integer>>();
    
        public List<List<Integer>> findSubsequences(int[] nums) {
            search(nums, -1, new Stack<Integer>());
            return reList;
        }
    
        private final void search(int[] nums, int point, Stack<Integer> stack) {
            int pre = stack.size() == 0 ? Integer.MIN_VALUE : stack.get(stack.size() - 1);
            Set<Integer> set = new HashSet<Integer>();
            for (int i = point + 1; i < nums.length; i++) {
                if (nums[i] < pre || set.contains(nums[i])) {
                    continue;
                }
                stack.push(nums[i]);
                set.add(nums[i]);
                if (stack.size() > 1) {
                    reList.add(new ArrayList<Integer>(stack));
                }
                search(nums, i, stack);
                stack.pop();
            }
        }

      JS:

    /**
     * @param {number[]} nums
     * @return {number[][]}
     */
    var findSubsequences = function (nums) {
        reArr = [];
        search(nums, -1, []);
        return reArr;
    };
    
    var reArr;
    
    var search = function (nums, point, reNum) {
        let pre = reNum.length == 0 ? -101 : reNum[reNum.length - 1];
        let set = new Set();
        for (let i = point + 1; i < nums.length; i++) {
            if (nums[i] < pre || set.has(nums[i])) {
                continue;
            }
            reNum.push(nums[i]);
            set.add(nums[i]);
            if (reNum.length > 1) {
                reArr.push(reNum.slice());
            }
            search(nums, i, reNum);
            prePop = reNum.pop();
        }
    }

    当你看清人们的真相,于是你知道了,你可以忍受孤独
  • 相关阅读:
    redis全量复制和部分复制
    tp5怎么使用find_in_set
    ms1
    nginx+php上传大文件配置
    培训第一天!
    PHP中使用CURL(五)
    PHP中使用CURL(四)
    PHP中使用CURL(三)
    PHP中使用CURL(二)
    PHP中使用CURL(一)
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13532785.html
Copyright © 2020-2023  润新知