[leetcode91]Restore IP Addresses(java)

原创 2015年07月10日 12:46:41

问题描述:
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地址最大为3个字符)
并利用剪枝法,对不符合条件的不再继续。
很显然,若抽象成树,那需要使用DFS。当递归查询到最后一个部分(4)时,若满足条件,则将字符串添加到list中,如果不满足条件,简单的返回。

还有要注意,IP地址要有效的条件!

代码如下:

import java.util.ArrayList;
public class ResoreIpAddr {
    private boolean isValid(String s){
        int length = s.length();
        if(length>=2&&s.charAt(0)=='0')
            return false;
        int nums = 0;
        for(int i = 0;i<length;i++)
            nums = nums*10+s.charAt(i)-'0';
        if(nums>255)
            return false;

        return true;
    }
    private void dfs(String s,String tmp,ArrayList<String> list,int count){
        if(count>=3){
            if(isValid(s))
                list.add(tmp+s);
            return;
        }
        String tmpString;
        for(int i = 1;i<4&&i<s.length();i++){
            tmpString = s.substring(0, i);
            if(isValid(tmpString)){
                dfs(s.substring(i), tmp+tmpString+'.', list, count+1);
            }
        }
    }
    public ArrayList<String> restoreIpAddresses(String s) {
        ArrayList<String> list = new ArrayList<String>();
        if(s.length()<4||s.length()>12)
            return list;
        dfs(s, "", list, 0);
        return list;
    }

    public void test(){
        ArrayList<String> list = restoreIpAddresses("2552551117");
        int count = list.size();
        for(int i = 0;i<count;i++){
            System.out.println(list.get(i));
        }
    }
}

相关文章推荐

leetcode:Restore IP Addresses 【Java】

一、问题描述 Given a string containing only digits, restore it by returning all possible valid IP addres...

[LeetCode][Java] Restore IP Addresses

题目: Given a string containing only digits, restore it by returning all possible valid IP addres...

[JAVA]LeetCode93 Restore IP Addresses

[JAVA]LeetCode93 Restore IP Addresses
  • fumier
  • fumier
  • 2015年05月22日 09:46
  • 366

LeetCode-93. Restore IP Addresses (JAVA)(重新组合IP)

LeetCode-93. Restore IP Addresses (JAVA)(重新组合IP)

【leetcode】String——Restore IP Addresses (93)

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

Leetcode--Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combina...
  • yskyj
  • yskyj
  • 2014年10月12日 21:23
  • 247

leetcode:Restore IP Addresses

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

leetcode Restore IP Addresses

好久没有更新了。最近在看stl源码解析。所以没有太做题了。顺便说下这本书,乍一看感觉很难,仔细看几天收获还是不错。只能感叹c++泛型机制好流弊。。。leetcode上面也就做了50个。 Given...

Restore IP Addresses [Leetcode 解题报告]

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

[C++]LeetCode: 40 Restore IP Addresses

题目: Given a string containing only digits, restore it by returning all possible valid IP address c...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode91]Restore IP Addresses(java)
举报原因:
原因补充:

(最多只允许输入30个字)