leetcode-Restore IP Addresses-ZZ

http://www.cnblogs.com/remlostime/archive/2012/11/14/2770072.html

 

 1 class Solution {
 2 private:
 3     vector<string> ret;
 4     int pos[4];
 5 public:
 6     bool check(string &s, int beg, int end)
 7     {
 8         string ip(s, beg, end - beg + 1);
 9         if (ip.size() == 1)
10             return "0" <= ip && ip <= "9";
11         else if (ip.size() == 2)
12             return "10" <= ip && ip <= "99";
13         else if (ip.size() == 3)
14             return "100" <= ip && ip <= "255";
15         else
16             return false;
17     }
18     
19     void dfs(int dep, int maxDep, string &s, int start)
20     {
21         if (dep == maxDep)
22         {
23             if (start == s.size())
24             {
25                 int beg = 0;
26                 string addr;
27                 for(int i = 0; i < maxDep; i++)
28                 {
29                     string ip(s, beg, pos[i] - beg + 1);
30                     beg = pos[i] + 1;
31                     addr += i == 0 ? ip : "." + ip;
32                 }
33                 ret.push_back(addr);    
34             }
35             return;
36         }
37         
38         for(int i = start; i < s.size(); i++)
39             if (check(s, start, i))
40             {
41                 pos[dep] = i;
42                 dfs(dep + 1, maxDep, s, i + 1);               
43             }
44     }
45     
46     vector<string> restoreIpAddresses(string s) {
47         // Start typing your C/C++ solution below
48         // DO NOT write int main() function
49         ret.clear();
50         dfs(0, 4, s, 0);
51         return ret;
52     }
53 };

 

 

 

 

http://yucoding.blogspot.com/2013/04/leetcode-question-83-restore-ip.html

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)


Analysis:
This problem can be viewed as a DP problem. There needed 3 dots to divide the string, and make sure the IP address is valid:  less than or equal to 255, greater or equal to 0, and note that, "0X" or "00X" is not valid.
For the DP, the length of each part is from 1 to 3. We use a vector<string> to store each part, and cut the string every time. Details see the code.

Note that "atoi" is for c-string, <string> need to convert to cstring by str.c_str();

Code(Updated 201309): 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
    bool valid(string s){
        if (s.size()==3 && (atoi(s.c_str())>255 || atoi(s.c_str())==0)){return false;}
        if (s.size()==3 && s[0]=='0'){return false;}
        if (s.size()==2 && atoi(s.c_str())==0){return false;}
        if (s.size()==2 && s[0]=='0'){return false;}
        return true;
    }
 
    void getRes(string s, string r, vector<string> &res, int k){
        if (k==0){
            if (s.empty()){res.push_back(r);}
            return;
        }else{
            for (int i=1;i<=3;i++){
                if (s.size()>=i && valid(s.substr(0,i))){
                    if (k==1){getRes(s.substr(i),r+s.substr(0,i),res,k-1);}
                    else{getRes(s.substr(i),r+s.substr(0,i)+".",res,k-1);}
                }
            }
        }
    }
 
    vector<string> restoreIpAddresses(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> res;
        getRes(s,"",res,4);
        return res;
    }
};

Code(old version):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
 
    void dp(string s,vector<string> &cur ,vector<string> &res){              
        if (cur.size()==3){ // if there are 4 parts in the original string
            cur.push_back(s); //all 4 parts and check if valid
            bool r = true;
            for (int i=0;i<4;i++){
                if (atoi(cur[i].c_str())>255){  //check value
                    r = false;
                    break;
                }
                if ((cur[i].size()>1 && cur[i][0]=='0')){ //check "0X" "00X" and "0XX" cases
                    r =false;
                    break;
                }
            }       
            if (r){
                res.push_back(cur[0]+"."+cur[1]+"."+cur[2]+"."+cur[3]);
            }
            cur.pop_back();
             
        }else{
            for (int i=0;i<3;i++){
                if (s.size()>i+1){
                    cur.push_back(s.substr(0,i+1));
                    dp(s.substr(i+1,(s.size()-i-1)),cur,res);
                    cur.pop_back();
                }
            }
        }
         
    }
 
 
    vector<string> restoreIpAddresses(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<string> res,cur;
        if (s.size()>12 || s.size()<4 ){return res;}
        dp(s,cur,res); // cur stores the current separation
        return res;
    }
};

 

posted @ 2014-10-15 16:53  Ryan in C++  阅读(312)  评论(0编辑  收藏  举报