Increasing Subsequences
题目地址:
https://leetcode.com/problems/increasing-subsequences/description/
题目:
解题思路:
传统backtracking解法。
代码:
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:
- 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.
解题思路:
传统backtracking解法。
代码:
public List<List<Integer>> findSubsequences(int[] nums) { Set<List<Integer>> rst= new HashSet<List<Integer>>(); List<Integer> list = new ArrayList<Integer>(); findSequence(rst, list, 0, nums); List result = new ArrayList(rst); return result; } public void findSequence(Set<List<Integer>> rst, List<Integer> list, int pos, int[] nums) { if (list.size() >= 2) { rst.add(new ArrayList(list)); } for (int i = pos; i < nums.length; i++) { if(list.size() == 0 || list.get(list.size() - 1) <= nums[i]) { list.add(nums[i]); findSequence(rst, list, i + 1, nums); list.remove(list.size() - 1); } } }

Comments
Post a Comment