这道题用hashset存放list,这样就不会出现重复的情况。如果array里面任何数都不相同,那么不用Set应该也可以
public class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
// using hash set to remove duplicate element
Set<List<Integer>> res= new HashSet<List<Integer>>();
helper(nums, 0, res, new ArrayList<Integer>());
List result = new ArrayList(res);
return result;
}
private void helper(int[] nums, int start, Set<List<Integer>> res, List<Integer> incr) {
if (incr.size() > 1) {
res.add(new ArrayList<>(incr));
}
for (int i = start; i < nums.length; i++) {
if (incr.size() == 0 || incr.get(incr.size() - 1) <= nums[i]) {
incr.add(nums[i]);
helper(nums, i + 1, res, incr);
incr.remove(incr.size() - 1);
}
}
}
}
本文介绍了一种使用HashSet去除数组中重复递增子序列的方法,并提供了一个具体的Java实现案例。通过递归的方式找到所有可能的递增子序列,并利用HashSet确保最终结果中不包含重复的子序列。
1727

被折叠的 条评论
为什么被折叠?



