zoukankan      html  css  js  c++  java
  • 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();
        }
    }

    当你看清人们的真相,于是你知道了,你可以忍受孤独
  • 相关阅读:
    oracle取字符串长度的函数length()和hengthb()
    文件操作
    numpy 库使用
    numpy 与 matplotlib 的应用过程
    使用numpy与matplotlib.pyplot画图
    面向对象的解读
    Python PIL
    Note of Jieba
    python 游戏 —— 汉诺塔(Hanoita)
    有进度条圆周率Π计算
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13532785.html
Copyright © 2011-2022 走看看