[LeetCode][Java] 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)

题意:

给定一个只包含数字的字符串,返回有这个字符串构成的所有的有效的IP地址。

比如:

给定"25525511135",

返回["255.255.11.135",
"255.255.111.35"]
.(顺序不计)

算法分析:

   *  IP分为4段,每一段的值域为0-255,也就是说每一位可能是1位,2位或者3位数字

* 因此对于一个给定的数字串,我们需要确定4个位置上的值,每个位置有三种可能的位数取值,

* 因此总共要考虑3^4=81中可能IP段划分方案。

利用循环递归解决子问题。对于每个段内数来说,最多3位最少1位,所以在每一层可以循环3次,来尝试填段。因为IP地址最多4个分段,当层数是3的时候说明已经尝试填过3个段了,那么把剩余没填的数段接到结尾即可。这个过程中要保证的是填的数是合法的,最后拼接的剩余的数也是合法的。

注意开头如果是0的话要特殊处理,如果开头是0,判断整个串是不是0,不是的话该字符就是非法的。因为001,01都是不对的。

AC代码:

<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
    public ArrayList<String> restoreIpAddresses(String s)
    {
        ArrayList<String> res = new ArrayList<String>();
        String item = new String();
        if (s.length()<4||s.length()>12)
        return res;  

        dfs(s, 0, item, res);
        return res;
    }  

    public void dfs(String s, int start, String item, ArrayList<String> res)
    {
        if (start == 3 && isValid(s))
        {
            res.add(item + s);
            return;
        }
        for(int i=0; i<3 && i<s.length()-1; i++)
        {
            String substr = s.substring(0,i+1);
            if (isValid(substr))
                dfs(s.substring(i+1, s.length()), start+1, item + substr + '.', res);
        }
    }  

    public boolean isValid(String s)
    {
        if (s.charAt(0)=='0')
            return s.equals("0");
        int num = Integer.parseInt(s);
        if(num <= 255 && num > 0)
            return true;
        else
            return false;
    }
}</span>

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-11-02 07:27:48

[LeetCode][Java] Restore IP Addresses的相关文章

【leetcode】Restore IP Addresses

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 mat

Java for LeetCode 093 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) 解题思路: 使用循环即可解决,

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 ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 给一串数字字符串,返回所有可能的

[LeetCode]46. Restore IP Addresses复原IP地址

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) Subscribe to se

leetcode No93. Restore IP Addresses

Question: 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) 判断IP地

【leetcode】Restore IP Addresses (middle)

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) 思路:回溯法 解向量X={str

【LeetCode】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) [解析] 题意:把一

leetCode 93.Restore IP Addresses (恢复IP地址) 解题思路和方法

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) 思路:本题用递归实现,一个ip

LeetCode 93. Restore IP Addresses 20170705 部分之前做了没写的题目

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) 题目大意:给出一串数字组成的字符