93. Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",
return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

总结见:http://www.jianshu.com/p/883fdda93a66

Solution1a:Backtracking(DFS)-a

思路:回溯(类似DFS) cur_tmp_str使用不同的string(即占用不同的内存: code中prefix + letters.charAt(i)),则不需要退后remove的步骤。
Time Complexity: 4^3 *O(n)copyToResult Space Complexity: O(n)

Solution1b:Backtracking(DFS)-b

思路:回溯(类似DFS) cur_tmp_str使用同一的string(占用相同的内存),通过StringBuilder实现,DFS到底 后通过remove后退。
Time Complexity: 4^3 *O(n)copyToResult Space Complexity: O(n)

Solution2:Iterative

Time Complexity: 4^3 *O(n)isValid Space Complexity: O(n)

Solution1a Code:

class Solution {
    public List restoreIpAddresses(String s) {
        List result = new ArrayList();
        String cur_res = "";
        restoreIp(s, 0, 0, cur_res, result);
        return result;
    }

    private void restoreIp(String ip, int idx, int count, String cur_res, List result) {
        if (count > 4) return;
        if (count == 4 && idx == ip.length()) {
            result.add(cur_res);
        }

        for (int len = 1; len <= 3; len++) { //next potential choise
            if (idx + len > ip.length()) break;
            String next = ip.substring(idx, idx + len);
            if(!isValid(next)) continue;
            restoreIp(ip, idx + len, count + 1, cur_res + next + (count==3 ? "" : "."), result);
        }
    }

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

Solution1b Code:

class Solution {
    public List restoreIpAddresses(String s) {
        List result = new ArrayList();
        StringBuilder cur_res = new StringBuilder();
        restoreIp(s, 0, 0, cur_res, result);
        return result;
    }

    private void restoreIp(String ip, int idx, int count, StringBuilder cur_res, List result) {
        if (count > 4) return;
        if (count == 4 && idx == ip.length()) {
            result.add(cur_res.toString());
        }

        for (int len = 1; len <= 3; len++) { //next potential choise
            if (idx + len > ip.length()) break;
            String next = ip.substring(idx, idx + len);
            if(!isValid(next)) continue;

            cur_res.append(next).append(count == 3 ? "" : ".");
            restoreIp(ip, idx + len, count + 1, cur_res, result);
            
            // step back
            cur_res.delete(cur_res.length() - (count == 3 ? next.length() : next.length() + 1), cur_res.length());
            
        }
    }

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

Solution2 Code:

class Solution {
    public List restoreIpAddresses(String s) {
        List result = new ArrayList();
        int len = s.length();
        for(int i = 1; i <= 3 && i <= len - 3; i++){
            for(int j = i + 1; j <= i + 3 && j <= len - 2; j++){
                for(int k = j + 1; k <= j + 3 && k <= len - 1; k++){
                    String s1 = s.substring(0,i), s2 = s.substring(i,j), s3 = s.substring(j,k), s4 = s.substring(k,len);
                    if(isValid(s1) && isValid(s2) && isValid(s3) && isValid(s4)){
                        result.add(s1 + "." + s2 + "." + s3 + "." + s4);
                    }
                }
            }
        }
        return result;
    }
    private boolean isValid(String s) {
        if(s.length() == 0 || s.length() > 3 || (s.charAt(0) == '0' && s.length() > 1) || Integer.parseInt(s) > 255) {
            return false;
        }
        return true;
    }
}

你可能感兴趣的:(93. Restore IP Addresses)