LeetCode刷题实战491:递增子序列
共 1468字,需浏览 3分钟
·
2022-01-09 20:49
示例
示例 1:
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]
解题
public class Solution {
public List> findSubsequences(int[] nums) {
Set> res = new HashSet
>();
helper(res, new ArrayList(), nums, 0);
return new ArrayList>(res);
}
private void helper(Set> res, List
) {subList, int[] nums, int start
if (subList.size() >= 2) {
res.add(new ArrayList(subList));
}
for (int i = start; i < nums.length; i++) {
if (subList.size() == 0 || subList.get(subList.size() - 1) <= nums[i]) {
subList.add(nums[i]);
helper(res, subList, nums, i + 1);
subList.remove(subList.size() - 1);
}
}
}
}