首页 > 代码库 > 491. Increasing Subsequences
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: The length of the given array will not exceed 15. The range of integer in the given array is [-100,100]. The given array may contain duplicates,
and two equal integers should also be considered as a special case of increasing sequence.
带重复节点的subset
public List<List<Integer>> findSubsequences(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); List<Integer> list = new ArrayList<>(); Arrays.sort(nums); helper(nums, 0, ans, list); return ans; } private void helper(int[] nums, int pos, List<List<Integer>> ans, List<Integer> list) { if (list.size() >= 2) { ans.add(new ArrayList(list)); } for (int i = pos; i < nums.length; i++) { if (i != pos && nums[i - 1] == nums[i]){ continue; } list.add(nums[i]); helper(nums, i + 1, ans, list); list.remove(list.size() - 1); } }
491. Increasing Subsequences
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。