资讯专栏INFORMATION COLUMN

[LintCode/LeetCode] Restore IP Addresses

bingo / 2991人阅读

摘要:第一个分割点第二个分割点第三个分割点

Problem

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

Example

Given "25525511135", return

[
  "255.255.11.135",
  "255.255.111.35"
]

Order does not matter.

Solution

Ordinary DFS Method

public class Solution {
    public ArrayList restoreIpAddresses(String s) {
        ArrayList res = new ArrayList();
        if (s.length() < 4 || s.length() > 12) {
            return res;
        }
        dfs(s,"", res, 0);
        return res;
    }
    public void dfs(String s, String temp, ArrayList res, int count) {
       if (count == 3 && isvalid(s)) {
           res.add(temp + s);
           return;
       }
       for (int i = 1; i < 4 && i < s.length(); i++) {
           String substr = s.substring(0, i);
           if (isvalid(substr)) {
               dfs(s.substring(i), temp + substr + ".", res, count+1);
           }
       }
    }
    public boolean isvalid(String s) {
        if (s.charAt(0) == "0") {
            return s.equals("0");
        }
        int num = Integer.parseInt(s);
        return num > 0 && num <= 255;
    }
}

Advanced DFS Method

public class Solution {
    private List res;
    public List restoreIpAddresses(String s) {
        res = new ArrayList();
        String cur = "";
        dfs(s, cur, -1, 0);
        return res;
    }
    public void dfs(String s, String cur, int index, int len) {
        if (len == 4 && index == s.length()-1) {
            res.add(cur.substring(0, cur.length()-1));
            return;
        }
        int temp = 0;
        if (s.length()-1-index > 3*(4-len)) return;
        for (int i = 1; i <= 3; i++) {
            if (index+i >= s.length()) break;
            if (i == 1 && s.charAt(index+1) == "0") {
                dfs(s, cur+"0.", index+1, len+1);
                break;
            }
            temp = temp*10 + s.charAt(index+i)-"0";
            if (temp <= 255) dfs(s, cur + temp + ".", index+i, len+1);
        }
    }
}

Optimized Above DFS Method

public class Solution {
    public List restoreIpAddresses(String s) {
        List res = new ArrayList<>();
        dfs(s, 0, "", res);
        return res;
    }
    private void dfs(String s, int index, String cur, List res) {
        if (index == 4) res.add(cur.substring(0, cur.length() - 1));
        else {
            for (int i = 0; i < 3; i++) {
                if (s.length() - i - 1 < 3 - index || s.length() - i - 1 > (3 - index) * 3) continue;
                if (i > 0 && s.charAt(0) == "0") break;
                if (i == 2 && s.substring(0, 3).compareTo("255") > 0) break;
                dfs(s.substring(i + 1), index + 1, cur + s.substring(0, i + 1) + ".", res);
            }
        }
    }
}

A simple way... O(n^3)

public class Solution {
    public List restoreIpAddresses(String s) {
        List res = new LinkedList();
        int len = s.length();
        // 第一个分割点
        for(int i = 1; i < 4 && i < len - 2; i++){
            // 第二个分割点
            for(int j = i + 1; j < i + 4 && j < len - 1; j++){
                // 第三个分割点
                for(int k = j + 1; k < j + 4 && k < len ; k++){
                    String s1 = s.substring(0,i), s2 = s.substring(i, j), s3 = s.substring(j, k), s4 = s.substring(k, s.length());
                    if(isValid(s1)&&isValid(s2)&&isValid(s3)&&isValid(s4)) res.add(s1+"."+s2+"."+s3+"."+s4);
                }
            }
        }
        return res;
    }
    private boolean isValid(String sub){
        return sub.length()<=3 && ((sub.charAt(0) != "0" && Integer.valueOf(sub) <=255) || sub.equals("0"));
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65470.html

相关文章

  • LeetCode: 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 [2...

    Shisui 评论0 收藏0
  • 93. Restore IP Addresses

    摘要:第一种解法,找出第一部分合法的剩余部分变成相似子问题。这里的特性是最大数字不能超过。比上个方法好的地方在于才会判断数字是否合法,避免了很多这种不需要检查的情况。 Given a string containing only digits, restore it by returning all possible valid IP address combinations. For e...

    andong777 评论0 收藏0
  • leetcode93. Restore IP Addresses

    摘要:题目要求返回字符串能够组成的所有地址。思路与代码地址由位二进制数字构成,一共分为个区间,每个区间位。那么我们只要划分出这四个区间,然后判断这四个区间的值是否符合标准即可。 题目要求 Given a string containing only digits, restore it by returning all possible valid IP address combinatio...

    chenjiang3 评论0 收藏0
  • [LeetCode] 93. Restore IP Addresses

    Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example: Input: 25525511135Output: [255.255.11.135, 255.255.111.35] Solution class So...

    xingqiba 评论0 收藏0
  • leetcode-93-Restore IP Addresses

    摘要:题目描述题目理解将一段字符广度搜索截取,分别有种组合形式,添加限制条件,过滤掉不适合的组合元素。长度,大小,首字母应用如果进行字符串的子元素组合穷举,可以应用。所有的循环,利用到前一个状态,都可以理解为动态规划的一种分支 题目描述:Given a string containing only digits, restore it by returning all possible va...

    wmui 评论0 收藏0

发表评论

0条评论

bingo

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<