給定一個整型數組, 你的任務是找到全部該數組的遞增子序列,遞增子序列的長度至少是2。java
示例:數組
輸入: [4, 6, 7, 7]
輸出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
說明:ide
給定數組的長度不會超過15。
數組中的整數範圍是 [-100,100]。
給定數組中可能包含重複數字,相等的數字應該被視爲遞增的一種狀況。spa
PS:
DFScode
class Solution { public List<List<Integer>> findSubsequences(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); dfs(nums, new ArrayList<>(), ans, 0, -101); return ans; } private void dfs(int[] nums, List<Integer> list, List<List<Integer>> ans, int index, int lastNum) { if (list.size() > 1) { ans.add(new ArrayList<>(list)); } for (int i = index; i < nums.length; i++) { if (nums[i] < lastNum) { continue; } boolean repeat = false; for (int j = index; j <= i - 1; j++) { if (nums[i] == nums[j]) { repeat = true; break; } } if (repeat) { continue; } list.add(nums[i]); dfs(nums, list, ans, i + 1, nums[i]); list.remove(list.size() - 1); } } }