题目描述:

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地址的组成:

1) 每个地址由4个部分组成,由3个'.'分割;

2) 每个部分小于等于255。

清楚这两点我们就可以开始解题了,这题我用了递归的方法来解题:

1) 首先用一个数count来计数添加了几个部分和一个数beg来记录上一个部分的终止位置,如果count<3的话就遍历往下,看看哪个字串符合IP地址组成的第二个条件,符合就继续递归,不符合就直接结束。

2) 最后当count==3时,直接取字符串的最后一部分进行比较,符合添加到数组,不符合就结束程序。这样就满足了两个条件。

代码:

 1 class Solution {
 2 public:
 3     vector<string> restoreIpAddresses(string s) {
 4         vector<string> ret;
 5         select(s, ret, "", 0, 0, s.size());
 6         return ret;
 7     }
 8     bool substrCheck(string str, string &ret, int count){
 9     //检查字串是否符合添加条件,符合就顺便添加了
10         unsigned num = 0, len = str.size();
11         if(len > 1 && str[0] == '0')
12             return false;
13         for(int i = 0; i < len; i++){
14             num = num * 10 + str[i] - '0';
15         }
16         if(num <= 255){
17             if(count)
18                 ret += '.' + str;
19             else
20                 ret += str;
21             return true;
22         }
23         return false;
24     }
25     void select(string &s, vector<string> &ret, string tempS, int count, int beg, int len){
26         if(count == 3){
27         //结束条件
28             if(substrCheck(s.substr(beg, len-beg), tempS, count)){
29                 ret.push_back(tempS);
30             }
31             return;
32         }
33         for(int i = beg+1; i < len; i++){
34         //遍历寻找下一个部分
35             string t = tempS;
36             if(substrCheck(s.substr(beg, i-beg), t, count))
37                 select(s, ret, t, count+1, i, len);
38             else
39                 break;
40         }
41     }
42 };

 

posted on 2018-04-17 16:30  宵夜在哪  阅读(102)  评论(0编辑  收藏  举报