代码随想录算法训练营二十八天 第七章 | 93.复原IP地址 78.子集 90.子集II

代码随想录算法训练营二十八天 第七章 | 93.复原IP地址 78.子集 90.子集II

93.复原IP地址

class Solution {
    public List<String> result = new ArrayList<>();
    public StringBuffer str = new StringBuffer();
    public List<String> restoreIpAddresses(String s) {
        if (s.length() > 12) return result;
        test(s, 0, 0);
        return result;
    }
    // num是添加逗号的数量
    public void test(String s, int startIndex, int num) {
        if (num == 3) {
            if (isfuhe(s, startIndex, s.length() - 1)){
                result.add(s);
            }
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isfuhe(s, startIndex, i)) {
                s = s.substring(0, i+1) + "." + s.substring(i+1);
                num++;
                test(s,i+2,num);
                num--;
                s = s.substring(0, i+1) + s.substring(i+2);
            } else {
                break;
            }
        }
    }

    public boolean isfuhe(String s, int start, int end) {
        if (start > end) {
            return false;
        }
        if (s.charAt(start) == '0' && start != end) {
            return false;
        }
        int num = 0;
        for (int i = start; i <= end; i++) {
            if (s.charAt(i) > '9' || s.charAt(i) < '0') {
                return false;
            }
            num = num * 10 + (s.charAt(i) - '0');
            if (num > 255) {
                return false;
            }
        }
        return true;
    }
}

78.子集

class Solution {
    public List<List<Integer>> result = new ArrayList<>();
    public LinkedList<Integer> paths = new LinkedList<>();
    public List<List<Integer>> subsets(int[] nums) {
        result.add(new ArrayList<>());
        test(nums,0);
        return result;
    }
    public void test(int[] nums, int startIndex) {
        
        for (int i = startIndex; i < nums.length; i++) {
            paths.add(nums[i]);
            result.add(new ArrayList<>(paths));
            test(nums, i+1);
            paths.removeLast();
        }
    }
}

90.子集II

class Solution {
    public List<List<Integer>> result = new ArrayList<>();
    public LinkedList<Integer> paths = new LinkedList<>();

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        
        Arrays.sort(nums);
        test(nums, 0);
        return result;
    }

    public void test(int[] nums, int startIndex) {
        result.add(new ArrayList<>(paths));
        for (int i = startIndex; i < nums.length; i++) {

            // 跳过当前树层使用过的、相同的元素
            if (i > startIndex && nums[i] == nums[i - 1]) {
                continue;
            } 
            paths.add(nums[i]);
            
            test(nums, i+1);
            
            paths.removeLast();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值