【LeetCode】491.Increasing Subsequences(Medium)解题报告
2017-11-27 21:18
399 查看
【LeetCode】491.Increasing Subsequences(Medium)解题报告
题目地址:https://leetcode.com/problems/increasing-subsequences/description/
题目描述:
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 .
本题涉及到组合,但是有额外三个要求,每个组合多于一个元素,不重复(set解决),递增。做本题前最好先做下基本的组合的题目。
Solution:
Date:2017年11月27日
题目地址:https://leetcode.com/problems/increasing-subsequences/description/
题目描述:
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.
本题涉及到组合,但是有额外三个要求,每个组合多于一个元素,不重复(set解决),递增。做本题前最好先做下基本的组合的题目。
Solution:
class Solution { public List<List<Integer>> findSubsequences(int[] nums) { Set<List<Integer>> set = new HashSet<>(); findSub(set,nums,new ArrayList<>(),0); return new ArrayList<>(set); } public void findSub(Set<List<Integer>> set, int[] nums , List<Integer> tempList,int index){ if(tempList.size()>1){ set.add(new ArrayList<>(tempList)); } for(int i=index ; i<nums.length ; i++){ if(tempList.size()==0 || tempList.get(tempList.size()-1)<=nums[i]){ tempList.add(nums[i]); findSub(set,nums,tempList,i+1); tempList.remove(tempList.size() - 1); } } } }
Date:2017年11月27日
相关文章推荐
- 【LeetCode】285.Inorder Successor in BST(Medium)(带锁题)解题报告
- 【LeetCode】560.Subarray Sum Equals K(Medium)解题报告
- LeetCode解题报告 392. Is Subsequence [medium]
- LeetCode解题报告 309. Best Time to Buy and Sell Stock with Cooldown[medium]
- 【LeetCode】435.Non-overlapping Intervals(Medium)解题报告
- LeetCode解题报告 122. Best Time to Buy and Sell Stock II [medium]
- 【LeetCode】98. Validate Binary Search Tree(Medium)解题报告
- 【LeetCode】337.House Robber III(Medium)解题报告
- 【LeetCode】55.Jump Game(Medium)解题报告
- 【LeetCode】40.Combination Sum II(Medium)解题报告
- LeetCode解题报告 394. Decode String [medium]
- 【LeetCode】646.Maximum Length of Pair Chain(Medium)解题报告
- 【LeetCode】77.Combinations(Medium)解题报告
- 【LeetCode】47.Permutations II(Medium)解题报告
- 【LeetCode】289.Game of Life(Medium)解题报告
- 【LeetCode】80.Remove Duplicates from Sorted Array II(Medium)解题报告
- 【LeetCode】228.Summary Ranges(Medium)解题报告
- LeetCode解题报告 445. Add Two Numbers II [medium]
- 【LeetCode】413.Arithmetic Slices(Medium)解题报告
- LeetCode解题报告 55. Jump Game [medium]