当前位置:网站首页>Leetcode-78-subset

Leetcode-78-subset

2022-06-25 18:42:00 z754916067

subject

 Insert picture description here

Ideas

  1. A glance back , First add Again remove This solution .
  2. Pay attention to the , To prevent repetition , need index Record where the current has been added .

Code

    ArrayList<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
    
        List<Integer> ll = new ArrayList<>();
        // Start backtracking 
        back(nums,ll,0);
        return ans;
    }
    public void back(int[] nums,List<Integer> ll,int index){
    
        ans.add(new ArrayList(ll));
        for (int i=index;i<nums.length;i++){
    
            if(ll.contains(nums[i])){
    
                continue;
            }else {
    
                ll.add(nums[i]);
                back(nums,ll,i+1);
                ll.remove(ll.size()-1);
            }
        }
    }
原网站

版权声明
本文为[z754916067]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251828294088.html