-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0093.复原ip地址.cpp
77 lines (71 loc) · 1.96 KB
/
0093.复原ip地址.cpp
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
/*
* @lc app=leetcode.cn id=93 lang=cpp
*
* [93] 复原IP地址
*
* https://leetcode-cn.com/problems/restore-ip-addresses/description/
*
* algorithms
* Medium (45.14%)
* Likes: 166
* Dislikes: 0
* Total Accepted: 20.1K
* Total Submissions: 44.5K
* Testcase Example: '"25525511135"'
*
* 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
*
* 示例:
*
* 输入: "25525511135"
* 输出: ["255.255.11.135", "255.255.111.35"]
*
*/
// @lc code=start
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> restoreIpAddresses(string s)
{
if (s.length() > 12 || s.length() < 4) {
return {};
}
vector<string> ans;
aux(s, ans, "", 0, 0);
return ans;
}
void aux(const string& s, vector<string>& ans, string tmp, int n, int pos)
{
if (n == 3) {
if (pos >= s.length() || stoi(s.substr(pos)) > 255 || ((s.length() - pos) > 1 && s[pos] == '0')) {
return;
} else {
ans.push_back(tmp + "." + s.substr(pos));
}
}
if (pos + 1 < s.length()) {
if (n == 0) {
aux(s, ans, s.substr(pos, 1), n + 1, pos + 1);
} else {
aux(s, ans, tmp + "." + s.substr(pos, 1), n + 1, pos + 1);
}
}
if (pos + 2 < s.length() && s[pos] != '0') {
if (n == 0) {
aux(s, ans, s.substr(pos, 2), n + 1, pos + 2);
} else {
aux(s, ans, tmp + "." + s.substr(pos, 2), n + 1, pos + 2);
}
}
if (pos + 3 < s.length() && s[pos] != '0' && stoi(s.substr(pos, 3)) <= 255) {
if (n == 0) {
aux(s, ans, s.substr(pos, 3), n + 1, pos + 3);
} else {
aux(s, ans, tmp + "." + s.substr(pos, 3), n + 1, pos + 3);
}
}
}
};
// @lc code=end