day28 回溯

day28 回溯

复原ip地址

题目链接 : 复原Ip地址

题目描述

有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 ‘.’ 分隔。

例如:“0.1.2.201” 和 “192.168.1.1” 是 有效 IP 地址,但是 “0.011.255.245”、“192.168.1.312” 和 “[email protected]” 是 无效 IP 地址。
给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 ‘.’ 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。

解答

class Solution {
    List<String> res = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        if (s.length() > 12) return res;
        backTracing(s,0,0);
        return res;
    }
    void backTracing(String s , int startIndex , int pointNum) {
        if (pointNum == 3) {
            if (isValid(s , startIndex , s.length() -1)) {
                res.add(s);
            }
            return;
        }
        for (int i = startIndex ; i < s.length(); i++) {
            if (isValid(s, startIndex,i)) {
                s = s.substring(0,i +1) + "." + s.substring(i + 1);
                pointNum++;
                backTracing(s , i + 2 , pointNum);
                pointNum--;
                s = s.substring(0 , i + 1) + s.substring(i + 2);
            } else {
                break;
            }
        }
    }
    boolean isValid(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;
    }
 }

子集

题目链接: 子集

题目描述

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

解答

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

子集

题目: 子集

描述

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

解答

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new LinkedList<>();
    boolean[] used;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums.length == 0) {
            res.add(path);
            return res;
        }
        Arrays.sort(nums);
        used = new boolean[nums.length];
        subsetHelper(nums , 0);
        return res;
    }
    void subsetHelper(int[] nums , int startIndex) {
        res.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;
            subsetHelper(nums , i + 1);
            path.removeLast();
            used[i] = false;
        }
    }
}

你可能感兴趣的:(算法)