-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestoreIPAddress.cpp
More file actions
86 lines (74 loc) · 2.42 KB
/
Copy pathRestoreIPAddress.cpp
File metadata and controls
86 lines (74 loc) · 2.42 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* =====================================================================================
* Filename: RstoreIPAddress.cpp
*
* Description:
*
* Version: 1.0
* Created: 2015-03-12
* Revision: none
* Compiler: gcc
*
* Author: yixun
*
* =====================================================================================
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)
Hide Tags Backtracking String
解题思路:实际上是找到一个合法的切分,每一个字符都要参与,不能省略0,如果出现'00'在ip的某一段中,则不合法。
考虑010010,有两种切分:0.10.0.10, 0.100.1.0
*/
#include <vector>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class Solution {
private:
vector<string> getValidIpSeg(string s, int seg_num) {
vector<string> results;
if (s.length() == 0) {
return results;
}
if (seg_num == 1) {
if (s.length() > 1 && s[0] == '0') {
return results;
}
int val = atoi(s.c_str());
if (val >= 0 && val <= 255) {
results.push_back(s);
}
return results;
} else {
for (int i = 1; i <= 3 && i < s.length(); i++) {
string part = s.substr(0, i);
if (i > 1 && part[0] == '0') {
continue;
}
int val = atoi(part.c_str());
if (val >= 0 && val <= 255) {
//printf("try %s, suffix=%s, seg_num=%d\n", part.c_str(), s.substr(i).c_str(), seg_num);
vector<string> suffix_res = getValidIpSeg(s.substr(i), seg_num - 1);
for (int j = 0; j < suffix_res.size(); j++) {
results.push_back(part + "." + suffix_res[j]);
}
}
}
return results;
}
}
public:
vector<string> restoreIpAddresses(string s) {
return getValidIpSeg(s, 4);
}
};
int main() {
Solution s;
string str = "010010";
vector<string> resIp = s.restoreIpAddresses(str);
for (int i = 0; i < resIp.size(); i++) {
printf("%s\n", resIp[i].c_str());
}
}