Restore IP Addresses

来源:互联网 发布:汪国真经典诗文 淘宝 编辑:程序博客网 时间:2024/04/29 15:49

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)

Java代码:

public class Solution {    public List<String> restoreIpAddresses(String s) {         List<String> ans = new ArrayList<String>();    int len = s.length();    for (int i = 1; i <=3; ++i){  // first cut        if (len-i > 9) continue;                    for (int j = i+1; j<=i+3; ++j){  //second cut            if (len-j > 6) continue;                            for (int k = j+1; k<=j+3 && k<len; ++k){  // third cut                int a,b,c,d;                // the four int's seperated by "."                a = Integer.parseInt(s.substring(0,i));                  b = Integer.parseInt(s.substring(i,j)); // notice that "01" can be parsed into 1. Need to deal with that later.                c = Integer.parseInt(s.substring(j,k));                d = Integer.parseInt(s.substring(k));                if (a>255 || b>255 || c>255 || d>255) continue;                 String ip = a+"."+b+"."+c+"."+d;                if (ip.length()<len+3) continue;  // this is to reject those int's parsed from "01" or "00"-like substrings                ans.add(ip);            }        }    }    return ans;    }}


0 0
原创粉丝点击