78.子集,90集合II(数组中含有重复元素),491递增子序列
    78.子集

    1. private List<List<Integer>>res=new ArrayList<>();
    2. private List<Integer>singleRes=new LinkedList<>();
    3. public List<List<Integer>> subsets(int[] nums) {
    4. res.add(new ArrayList<>());
    5. backTrack(0,nums);
    6. return res;
    7. }
    8. private void backTrack(int index,int[] nums){
    9. if(singleRes.size()>0) {
    10. res.add(new ArrayList<>(singleRes));
    11. }
    12. if(index>=nums.length){
    13. return;
    14. }
    15. for(int i=index;i<nums.length;i++){
    16. singleRes.add(nums[i]);
    17. backTrack(i+1,nums);
    18. singleRes.remove(singleRes.size()-1);
    19. }
    20. }
    1. 90.子集2
    1. private List<List<Integer>>res=new ArrayList<>();
    2. private List<Integer>singleRes=new LinkedList<>();
    3. public List<List<Integer>> subsetsWithDup(int[] nums) {
    4. res.add(new ArrayList<>());
    5. Arrays.sort(nums);
    6. backTrack(0,nums);
    7. return res;
    8. }
    9. private void backTrack(int index,int[]nums){
    10. if(singleRes.size()>0){
    11. res.add(new ArrayList<>(singleRes));
    12. }
    13. if(index>=nums.length){
    14. return;
    15. }
    16. for(int i=index;i<nums.length;i++){
    17. if(i>index&&nums[i]==nums[i-1]){
    18. continue;
    19. }
    20. singleRes.add(nums[i]);
    21. backTrack(i+1,nums);
    22. singleRes.remove(singleRes.size()-1);
    23. }
    24. }

    491.递增子序列

    1. private List<List<Integer>>res=new ArrayList<>();
    2. private List<Integer>singleRes=new LinkedList<>();
    3. public List<List<Integer>> findSubsequences(int[] nums) {
    4. backTrack(0,nums,Integer.MIN_VALUE);
    5. return res;
    6. }
    7. private void backTrack(int index,int[]nums,int lastNum){
    8. if(singleRes.size()>=2){
    9. res.add(new ArrayList<>(singleRes));
    10. }
    11. if(index>=nums.length){
    12. return;
    13. }
    14. boolean[] vis = new boolean[201];
    15. for(int i=index;i<nums.length;i++){
    16. if(nums[i]<lastNum){
    17. continue;
    18. }
    19. if(vis[nums[i]+100]){
    20. continue;
    21. }
    22. singleRes.add(nums[i]);
    23. vis[nums[i]+100]=true;
    24. backTrack(i+1,nums,nums[i]);
    25. singleRes.remove(singleRes.size()-1);
    26. }
    27. }