代码随想录训练营第28天| 93.复原IP地址、78.子集、90.子集II

93.复原IP地址

题目链接:93. 复原 IP 地址 - 力扣(LeetCode)

class Solution {
    List<String> ans = new ArrayList<>();
    StringBuffer sb = new StringBuffer();
    public List<String> restoreIpAddresses(String s) {
        backtrack(s, 0, 0);
        return ans;
    }
    private void backtrack(String s,int n,int index) {
        if(n == 4 && index == s.length()) {
            ans.add(sb.toString().substring(0, sb.length() - 1));
            return;
        }else if(n == 4 || index == s.length()){
            return;
        }
        for(int i = index; i < s.length() && i < index + 3; ++i) {
            if(helper(s.substring(index, i + 1))) {
                sb.append(s.substring(index, i + 1));
                sb.append('.');
                ++n;
            }else {
                continue;
            }
            backtrack(s, n, i+1);
            sb.deleteCharAt(sb.length() - 1);
            --n;
            for(int j = sb.length() - 1; j >=0; --j) {
                if(sb.charAt(j) == '.') {
                    break;
                }else {
                    sb.deleteCharAt(j);
                }
            } 
        }
    }
    private boolean helper(String str) {
        if(str.length() == 1) {
            return true;
        }
        if(str.charAt(0) == '0' || str.length() > 3) {
            return false;
        }
        int b = 0;
        for(int i = 0; i < str.length(); ++i) {
            b *= 10;
            b += str.charAt(i) - '0';
        }
        return b <= 255;
    }
}

78.子集

题目链接:78. 子集 - 力扣(LeetCode)

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        backtrack(nums, 0, new ArrayList<>());
        return ans;
    }
    private void backtrack(int[] nums, int index,List<Integer> list) {
        ans.add(new ArrayList<>(list));
        for(int i = index; i < nums.length; ++i) {
            list.add(nums[i]);
            backtrack(nums, i + 1, list);
            list.removeLast();
        }
    }
}

90.子集II

题目链接:90. 子集 II - 力扣(LeetCode)

无剪枝:

class Solution {
    Set<List<Integer>> ans = new HashSet();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        backtrack(nums, 0,  new ArrayList<>());
        return new ArrayList<>(ans);
    }
    private void backtrack(int[] nums, int index,  List<Integer> list) {
        ans.add(new ArrayList<>(list));
        for(int i = index; i < nums.length; ++i) {
                list.add(nums[i]);   
            backtrack(nums, i + 1,  list);
            list.removeLast();
        }
    }
}

used数组剪枝:

class Solution {
   List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
   LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
   boolean[] used;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums.length == 0){
            result.add(path);
            return result;
        }
        Arrays.sort(nums);
        used = new boolean[nums.length];
        subsetsWithDupHelper(nums, 0);
        return result;
    }
    
    private void subsetsWithDupHelper(int[] nums, int startIndex){
        result.add(new ArrayList<>(path));
        if (startIndex >= nums.length){
            return;
        }
        for (int i = startIndex; i < nums.length; i++){
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            subsetsWithDupHelper(nums, i + 1);
            path.removeLast();
            used[i] = false;
        }
    }
}

不适用used数组剪枝:

class Solution {

  List<List<Integer>> res = new ArrayList<>();
  LinkedList<Integer> path = new LinkedList<>();
  
  public List<List<Integer>> subsetsWithDup( int[] nums ) {
    Arrays.sort( nums );
    subsetsWithDupHelper( nums, 0 );
    return res;
  }


  private void subsetsWithDupHelper( int[] nums, int start ) {
    res.add( new ArrayList<>( path ) );

    for ( int i = start; i < nums.length; i++ ) {
        // 跳过当前树层使用过的、相同的元素
      if ( i > start && nums[i - 1] == nums[i] ) {
        continue;
      }
      path.add( nums[i] );
      subsetsWithDupHelper( nums, i + 1 );
      path.removeLast();
    }
  }

}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值