• 491. Increasing Subsequences


    Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .

    Example:

    Input: [4, 6, 7, 7]
    Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
    

    Note:

    1. The length of the given array will not exceed 15.
    2. The range of integer in the given array is [-100,100].
    3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

    用backtracking,set去重

    time: O(2^n), space: O(n)

    class Solution {
        public List<List<Integer>> findSubsequences(int[] nums) {
            Set<List<Integer>> res = new HashSet<>();
            if(nums == null || nums.length == 0) return new ArrayList<>(res);
            dfs(nums, 0, new ArrayList<>(), res);
            return new ArrayList<>(res);
        }
        private void dfs(int[] nums, int idx, List<Integer> tmp, Set<List<Integer>> res) {
            if(tmp.size() >= 2 && !res.contains(tmp))
                res.add(new ArrayList<>(tmp));
    
            for(int i = idx; i < nums.length; i++) {
                if(tmp.size() == 0 || nums[i] >= tmp.get(tmp.size() - 1)) {
                    tmp.add(nums[i]);
                    dfs(nums, i + 1, tmp, res);
                    tmp.remove(tmp.size() - 1);
                }
            }
        }
    }
  • 相关阅读:
    spring事物配置,声明式事务管理和基于@Transactional注解的使用
    spring集成ehcache本地缓存
    Java并发编程:volatile关键字解析
    Callable接口、Runable接口、Future接口
    Sorting It All Out
    Borg Maze
    Agri-Net
    Highways
    Truck History
    Arbitrage
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10078532.html
Copyright © 2020-2023  润新知