第七章 回溯算法part04

50 阅读1分钟

93. Restore IP Addresses

valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.

  • For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245""192.168.1.312" and "192.168@1.1" are invalid IP addresses.

Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.

题目解析:

  • 回溯
  • 遍历时为了防止超出3位或超出字符串长度,可以i < Math.min(s.length(), start + 3)

代码:

class Solution {
    public List<String> restoreIpAddresses(String s) {
        List<String> result = new ArrayList<>();
        if (s.length() > 12 || s.length() < 4) return result;
        backtracking(s, 0, new ArrayList<>(), result);
        return result;
    }

    public void backtracking(String s, int start, List<String> comb, List<String> result) {
        if (start == s.length() && comb.size() == 4) {
            String ip = comb.stream().collect(Collectors.joining("."));
            result.add(ip);
        }
        if (start >= s.length() || comb.size() >= 4) {
            return;
        }

        for (int i = start; i < Math.min(s.length(), start + 3); i++) {
            String seg = s.substring(start, i+1);
            if (isValid(seg)) {
                comb.add(seg);
                backtracking(s, i+1, comb, result);
                comb.remove(comb.size() - 1);
            }
        }
    }

    public boolean isValid(String s) {
        if (s.charAt(0) == '0' && s.length() > 1) {
            return false;
        }
        if (Integer.parseInt(s) > 255) {
            return false;
        }
        return true;
    }
}

78. Subsets

Given an integer array nums of unique elements, return all possible subsets (the power set) .

The solution set must not contain duplicate subsets. Return the solution in any order.

代码:

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        backtracking(nums, 0, new ArrayList<>(), result);
        return result;
    }

    public void backtracking(int[] nums, int start, List<Integer> comb, List<List<Integer>> result) {
        result.add(new ArrayList<>(comb));
        if (start >= nums.length) return;
        for (int i = start; i < nums.length; i++ ) {
            comb.add(nums[i]);
            backtracking(nums, i + 1, comb, result);
            comb.remove(comb.size() - 1);
        }
    }
}

90. Subsets II

Given an integer array nums that may contain duplicates, return all possible subsets (the power set) .

The solution set must not contain duplicate subsets. Return the solution in any order.

题目解析:

  • 有重复元素一定要先排序

代码:

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        backtracking(nums, 0, new ArrayList<>());
        return result;
    }

    public void backtracking(int[] nums, int start, List<Integer> comb) {
        result.add(new ArrayList<>(comb));
        if (start >= nums.length) return;
        for (int i = start; i < nums.length; i++) {
            if (i > start && nums[i] == nums[i-1]) continue;
            comb.add(nums[i]);
            backtracking(nums, i + 1, comb);
            comb.remove(comb.size() - 1);
        }
    }
}