diff --git a/problems/0491.递增子序列.md b/problems/0491.递增子序列.md index 130b5666..4b21008e 100644 --- a/problems/0491.递增子序列.md +++ b/problems/0491.递增子序列.md @@ -205,6 +205,30 @@ public: ### Java +```Java +//using set, aligned with the unimproved method +class Solution { + List> result = new ArrayList<>(); + List path = new ArrayList<>(); + public List> findSubsequences(int[] nums) { + backTracking(nums, 0); + return result; + } + private void backTracking(int[] nums, int startIndex){ + if(path.size() >= 2) + result.add(new ArrayList<>(path)); + HashSet hs = new HashSet<>(); + for(int i = startIndex; i < nums.length; i++){ + if(!path.isEmpty() && path.get(path.size() -1 ) > nums[i] || hs.contains(nums[i])) + continue; + hs.add(nums[i]); + path.add(nums[i]); + backTracking(nums, i + 1); + path.remove(path.size() - 1); + } + } +} +``` ```java class Solution {