let 90 Subsets II

* 主题思想:* 这题解法是回溯法,典型的搜索题。 和permutation 以及subset是同样类型的题。 这题主要复杂在有相同数字,需要跳过

这里记录下一种通用的解法:

  1. 首先排序,
  2. 回溯法,搜索
  3. 搜索中根据题目要求,跳过相同数字

AC 代码:

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {

        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        // important  need sort first
        Arrays.sort(nums);
        backtrack(ans,new ArrayList<Integer>(),nums,0);
        return ans;
    }
    public void backtrack(List<List<Integer>> ans,List<Integer> tmp, int[] nums, int start){

        ans.add(new ArrayList<Integer>(tmp));
        for(int i=start;i<nums.length;i++){
           // here  skip duplicates
           if(i>start&&nums[i]==nums[i-1]) continue;
            tmp.add(nums[i]);
            backtrack(ans,tmp,nums,i+1);
            tmp.remove(tmp.size()-1);
        }
        return ;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值