[Leetcode 74] 92 Restore IP Addresses

Problem:

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)

 

Analysis:

Originally, I thought DFS may be a good solution. But since the brute-force enumeration is not that complicated, So I tried brute force method. The key observation is that:

1. to restore the IP address, we need to divide the original string into four sub-parts, thus need to choose 3 positions in the string to divide it.

2. for each substring, we need to judge whether it's valid or not. The rule is:

  a. the value of the substring should in the range of [0, 255]

  b. if it's not a 0, then no leading 0 is allowed. thus "010", "01", "00", "000" are invalid.

3. to get the final IP address, we need only concatinate as follows: s1 + "." + s2 + "." + s3 + "." + s4

 

For the dividing part, we can make use of some knowledge of IP address to reduce the work:

1. length of IP address should between 4 and 12 inclusively. Any given string's length less than 4 or greater than 12 can not be used as an IP address.

2. each sub-part's length should between 1 and 4 inclusively. Thus each for loop only need to check 3 division positions.

 

Code:

 1 class Solution {
 2 public:
 3     vector<string> restoreIpAddresses(string s) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         vector<string> res;
 7         
 8         if (s.length() < 4 || s.length() > 12)
 9             return res;
10             
11         int len = s.length();
12         
13         for (int i=1; i<4 && i<len; i++)
14             for (int j=i+1; j<i+4 && j<len; j++)
15                for (int k=j+1; k<j+4 && k<len; k++) {
16                        if (isValid(s, 0, i) && isValid(s, i, j) &&
17                             isValid(s, j, k) && isValid(s, k, len)) {
18                             string tmp = s.substr(0, i) + "." + 
19                                             s.substr(i, j-i) + "." +
20                                             s.substr(j, k-j) + "." + 
21                                             s.substr(k, len-k);
22                                 
23                             res.push_back(tmp);
24                         }
25                        
26                    }
27         
28         return res;
29     }
30     
31 private:
32     //  check sub-string s[s, e)'s validity
33     bool isValid(string &s, int a, int e) {
34         //only 1 bit, true anyway
35         if (a+1 == e)
36             return true;
37         
38         int val = 0;
39         bool leading = false;
40         for (int i=a; i<e; i++) {
41             if (leading) {
42                 val = val * 10 + s[i] - '0';
43             } else {
44                 if (s[i] == '0')
45                     return false;
46                 else {
47                     val = val * 10 + s[i]-'0';
48                     leading = true;
49                 }
50             }
51         }
52         
53         if (val<=255 && val>=0)
54             return true;
55         else
56             return false;
57     }
58 };
View Code

 

posted on 2013-07-22 12:53  freeneng  阅读(261)  评论(0编辑  收藏  举报

导航