提交 476e9c90 编写于 作者: 每日一练社区's avatar 每日一练社区

update dir sstructure

上级 38e70eca
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"反转链表 II"
],
"children": [],
"export": [
"solution.json"
],
"title": "反转链表 II"
}
\ No newline at end of file
给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code><code>right</code> ,其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出:</strong>[1,4,3,2,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [5], left = 1, right = 1<strong><br />输出:</strong>[5]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目为 <code>n</code></li> <li><code>1 <= n <= 500</code></li> <li><code>-500 <= Node.val <= 500</code></li> <li><code>1 <= left <= right <= n</code></li></ul><p> </p><p><strong>进阶:</strong> 你可以使用一趟扫描完成反转吗?</p>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
static struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
{
int i;
struct ListNode dummy;
struct ListNode *prev = &dummy;
prev->next = head;
for (i = 1; i < m; i++)
{
prev = prev->next;
}
struct ListNode *p = prev->next;
for (i = m; i < n; i++)
{
struct ListNode *q = p->next;
p->next = q->next;
q->next = prev->next;
prev->next = q;
}
return dummy.next;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "Usage: ./test m n 1 2 3...\n");
exit(-1);
}
int i, count = argc - 3;
struct ListNode dummy;
struct ListNode *prev = &dummy;
struct ListNode *p;
for (i = 0; i < count; i++)
{
p = malloc(sizeof(*p));
p->val = atoi(argv[i + 3]);
p->next = NULL;
prev->next = p;
prev = p;
}
int m = atoi(argv[1]);
int n = atoi(argv[2]);
struct ListNode *head = reverseBetween(dummy.next, m, n);
for (p = head; p != NULL; p = p->next)
{
printf("%d ", p->val);
}
printf("\n");
return 0;
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "34943e4274e24c9a9554a40c882fe5ce"
}
\ No newline at end of file
# 反转链表 II
给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code> 和 <code>right</code> ,其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点,返回 <strong>反转后的链表</strong> 。<p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入:</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出:</strong>[1,4,3,2,5]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>head = [5], left = 1, right = 1<strong><br />输出:</strong>[5]</pre><p> </p><p><strong>提示:</strong></p><ul> <li>链表中节点数目为 <code>n</code></li> <li><code>1 <= n <= 500</code></li> <li><code>-500 <= Node.val <= 500</code></li> <li><code>1 <= left <= right <= n</code></li></ul><p> </p><p><strong>进阶:</strong> 你可以使用一趟扫描完成反转吗?</p>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
```
### after
```cpp
```
## 答案
```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
ListNode *dummy = new ListNode(-1), *pre = dummy;
dummy->next = head;
for (int i = 0; i < m; ++i)
pre = pre->next;
ListNode *cur = pre->next;
for (int i = m; i < n - 1; ++i)
{
ListNode *t = cur->next;
cur->next = t->next;
t->next = pre->next;
pre->next = t;
}
return dummy->next;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
if (head == nullptr || head->next == nullptr || m == n)
return head;
ListNode *pre = nullptr, *cur = head;
while (m > 1)
{
pre = cur;
cur = cur->next;
m--;
n--;
}
ListNode *tail = cur, *con = pre;
ListNode *third;
while (n > 0)
{
third = cur->next;
cur->next = pre;
pre = cur;
cur = third;
n--;
}
if (con)
{
con->next = pre;
}
else
{
head = pre;
}
tail->next = cur;
return head;
}
};
```
### B
```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
if (head == NULL || m == n)
return head;
ListNode *pm = head, *pn = head;
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *pm0 = dummy;
while (m-- > 1)
pm = pm->next, pm0 = pm0->next;
while (n-- > 1)
pn = pn->next;
ListNode *pPre = pm, *pCur = pPre->next, *pn2 = pn->next;
while (pCur != pn2)
{
if (pCur)
{
ListNode *pNext = pCur->next;
pCur->next = pPre;
pPre = pCur;
pCur = pNext;
}
}
pm0->next = pn;
pm->next = pn2;
return dummy->next;
}
};
```
### C
```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
if ((head == NULL) || (head->next == NULL))
return head;
ListNode *first = new ListNode(0);
first->next = head;
int cnt = 1;
for (int i = 0; i < m - 1; ++i)
{
first = first->next;
cnt++;
}
ListNode *old_pre = first;
ListNode *cur = first->next;
ListNode *cur_next = cur->next;
ListNode *new_pre = cur;
ListNode *temp;
/*反转*/
while ((cur != NULL) && (cnt < n))
{
temp = cur_next->next;
cur_next->next = cur;
cur = cur_next;
cur_next = temp;
cnt++;
}
old_pre->next = cur;
new_pre->next = cur_next;
if (m == 1)
return cur;
else
return head;
}
};
```
{
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"keywords": [
"leetcode",
"复原 IP 地址"
],
"children": [],
"export": [
"solution.json"
],
"title": "复原 IP 地址"
}
\ No newline at end of file
<p>给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 <code>s</code> 获得的 <strong>有效 IP 地址 </strong>。你可以按任何顺序返回答案。</p><p><strong>有效 IP 地址</strong> 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 <code>0</code>),整数之间用 <code>'.'</code> 分隔。</p><p>例如:"0.1.2.201" 和 "192.168.1.1" 是 <strong>有效</strong> IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 <strong>无效</strong> IP 地址。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "25525511135"<strong><br />输出:</strong>["255.255.11.135","255.255.111.35"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "0000"<strong><br />输出:</strong>["0.0.0.0"]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "1111"<strong><br />输出:</strong>["1.1.1.1"]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "010010"<strong><br />输出:</strong>["0.10.0.10","0.100.1.0"]</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "101023"<strong><br />输出:</strong>["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3000</code></li> <li><code>s</code> 仅由数字组成</li></ul>
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool valid(char *ip, int len)
{
if (len > 1 && ip[0] == '0')
{
return false;
}
if (len == 3)
{
int n = (ip[0] - '0') * 100 + (ip[1] - '0') * 10 + (ip[2] - '0');
if (n > 255)
{
return false;
}
}
return true;
}
#define WIDTH 4
static void dfs(char *s, int start, char *stack, int num, char **results, int *count)
{
int i, j;
if (num == 4)
{
if (s[start] == '\0')
{
results[*count] = malloc(3 * 4 + 3 + 1);
char *p = results[*count];
for (j = 0; j < num; j++)
{
char *q = stack + j * WIDTH;
while ((*p++ = *q++) != '\0')
{
}
if (j != 3)
{
*(p - 1) = '.';
}
}
(*count)++;
}
}
else
{
char *p = stack + num * WIDTH;
char *q = p;
for (i = start; s[i] != '\0' && i < start + 3; i++)
{
*q++ = s[i];
*q = '\0';
if (!valid(p, q - p))
{
return;
}
dfs(s, i + 1, stack, num + 1, results, count);
if (num + 1 < 4)
{
memset(stack + (num + 1) * WIDTH, 0, WIDTH);
}
}
}
}
static char **restoreIpAddresses(char *s, int *returnSize)
{
int count = 0;
char **results = malloc(100 * sizeof(char *));
char addr[16] = {'\0'};
dfs(s, 0, addr, 0, results, &count);
*returnSize = count;
return results;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./test num\n");
exit(-1);
}
int i, count = 0;
char **list = restoreIpAddresses(argv[1], &count);
for (i = 0; i < count; i++)
{
printf("%s\n", list[i]);
}
}
\ No newline at end of file
{
"type": "code_options",
"author": "CSDN.net",
"source": "solution.md",
"exercise_id": "fac0e6d0d7f14dde895ebd26f693fda9"
}
\ No newline at end of file
# 复原 IP 地址
<p>给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 <code>s</code> 获得的 <strong>有效 IP 地址 </strong>。你可以按任何顺序返回答案。</p><p><strong>有效 IP 地址</strong> 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 <code>0</code>),整数之间用 <code>'.'</code> 分隔。</p><p>例如:"0.1.2.201" 和 "192.168.1.1" 是 <strong>有效</strong> IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 <strong>无效</strong> IP 地址。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>s = "25525511135"<strong><br />输出:</strong>["255.255.11.135","255.255.111.35"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>s = "0000"<strong><br />输出:</strong>["0.0.0.0"]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>s = "1111"<strong><br />输出:</strong>["1.1.1.1"]</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>s = "010010"<strong><br />输出:</strong>["0.10.0.10","0.100.1.0"]</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>s = "101023"<strong><br />输出:</strong>["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= s.length <= 3000</code></li> <li><code>s</code> 仅由数字组成</li></ul>
<p>以下错误的选项是?</p>
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector<string> res;
string s = "25525511135";
res = sol.restoreIpAddresses(s);
for (auto i : res)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
void dfs(vector<string> &res, string &s, int start, vector<string> &vec)
{
if (start == s.size())
{
if (vec.size() == 4)
{
string str;
int flag = 0;
for (int k = 0; k < 4; k++)
{
int num = atoi(vec[k].c_str());
if (num >= 0 && num <= 255)
{
str += vec[k];
}
if (k < 3)
{
str += '.';
}
}
if (flag == 0)
{
res.push_back(str);
}
}
return;
}
for (int i = start; i < s.size(); i++)
{
if (tmp != "0" && tmp.size() < 3)
{
tmp += s[i];
if (vec.size() < 4)
{
vec.push_back(tmp);
tmp = "";
dfs(res, s, i + 1, vec);
tmp = vec.back();
vec.pop_back();
}
}
}
}
vector<string> restoreIpAddresses(string s)
{
int n = s.size();
vector<string> res;
if (n < 4 || n > 12)
{
return res;
}
vector<string> vec;
dfs(res, s, 0, vec);
return res;
}
private:
string tmp;
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector<string> res;
vector<string> restoreIpAddresses(string s)
{
string temp = "";
dfs(s, temp, 0);
return res;
}
void dfs(string s, string &temp, int word_number)
{
if (word_number == 4)
{
if (s.empty())
res.push_back(temp);
}
else
{
if (word_number > 0)
temp += '.';
for (int i = 1; i <= 3 && i <= s.length(); i++)
{
if (valid(s.substr(0, i)))
{
temp += s.substr(0, i);
dfs(s.substr(i, s.length() - i), temp, word_number + 1);
temp = temp.substr(0, temp.length() - i);
}
}
temp.pop_back();
}
}
bool valid(const string &s)
{
if (s.empty() || (s[0] == '0' && s.size() > 1))
return false;
int val = stoi(s);
if (val >= 0 && val <= 255)
return true;
return false;
}
};
```
### B
```cpp
class Solution
{
public:
vector<string> res;
bool isOk(int cnt, int beg, int len, string &s)
{
if (s.size() - beg < 4 - cnt || s.size() - beg > 3 * (4 - cnt))
return false;
string str = s.substr(beg, len);
if (len == 3 && stoi(str) > 255)
return false;
if (len >= 2 && str[0] == '0')
return false;
return true;
}
void backtrack(int cnt, int beg, string str, string &s)
{
if (cnt == 3)
{
if (isOk(cnt, beg, s.size() - beg, s))
res.push_back(str + s.substr(beg, s.size() - beg));
return;
}
for (int i = 1; i <= 3; ++i)
{
if (isOk(cnt, beg, i, s))
backtrack(cnt + 1, beg + i, str + s.substr(beg, i) + ".", s);
}
}
vector<string> restoreIpAddresses(string s)
{
backtrack(0, 0, "", s);
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector<string> restoreIpAddresses(string s)
{
int n = s.size();
if (n > 12 || n < 4)
return vector<string>();
vector<string> res;
string ans;
for (int i = 1; i <= 3; ++i)
{
if (n - i < 3 || n - i > 9)
continue;
string s1 = s.substr(0, i);
if (i == 3 && stoi(s1) > 255)
continue;
if (i == 2 && s1[0] == '0' || i == 3 && s1[0] == '0')
continue;
for (int j = 1; j <= 3; ++j)
{
if (n - i - j < 2 || n - i - j > 6)
continue;
string s2 = s.substr(i, j);
if (j == 3 && stoi(s2) > 255)
continue;
if (j == 2 && s2[0] == '0' || j == 3 && s2[0] == '0')
continue;
for (int k = 1; k <= 3; ++k)
{
if (n - i - j - k < 1 || n - i - j - k > 3)
continue;
string s3 = s.substr(i + j, k);
if (k == 3 && stoi(s3) > 255)
continue;
if (k == 2 && s3[0] == '0' || k == 3 && s3[0] == '0')
continue;
string s4 = s.substr(i + j + k, n - i - j - k);
if (stoi(s4) > 255)
continue;
if (n - i - j - k == 2 && s4[0] == '0' || n - i - j - k == 3 && s4[0] == '0')
continue;
res.push_back(s1 + "." + s2 + "." + s3 + "." + s4);
}
}
}
return res;
}
};
```
...@@ -4,7 +4,15 @@ ...@@ -4,7 +4,15 @@
## aop ## aop
### before ### before
```cpp ```cpp
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
``` ```
### after ### after
```cpp ```cpp
...@@ -13,21 +21,147 @@ ...@@ -13,21 +21,147 @@
## 答案 ## 答案
```cpp ```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
ListNode *dummy = new ListNode(-1), *pre = dummy;
dummy->next = head;
for (int i = 0; i < m; ++i)
pre = pre->next;
ListNode *cur = pre->next;
for (int i = m; i < n - 1; ++i)
{
ListNode *t = cur->next;
cur->next = t->next;
t->next = pre->next;
pre->next = t;
}
return dummy->next;
}
};
``` ```
## 选项 ## 选项
### A ### A
```cpp ```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
if (head == nullptr || head->next == nullptr || m == n)
return head;
ListNode *pre = nullptr, *cur = head;
while (m > 1)
{
pre = cur;
cur = cur->next;
m--;
n--;
}
ListNode *tail = cur, *con = pre;
ListNode *third;
while (n > 0)
{
third = cur->next;
cur->next = pre;
pre = cur;
cur = third;
n--;
}
if (con)
{
con->next = pre;
}
else
{
head = pre;
}
tail->next = cur;
return head;
}
};
``` ```
### B ### B
```cpp ```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
if (head == NULL || m == n)
return head;
ListNode *pm = head, *pn = head;
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *pm0 = dummy;
while (m-- > 1)
pm = pm->next, pm0 = pm0->next;
while (n-- > 1)
pn = pn->next;
ListNode *pPre = pm, *pCur = pPre->next, *pn2 = pn->next;
while (pCur != pn2)
{
if (pCur)
{
ListNode *pNext = pCur->next;
pCur->next = pPre;
pPre = pCur;
pCur = pNext;
}
}
pm0->next = pn;
pm->next = pn2;
return dummy->next;
}
};
``` ```
### C ### C
```cpp ```cpp
class Solution
{
public:
ListNode *reverseBetween(ListNode *head, int m, int n)
{
if ((head == NULL) || (head->next == NULL))
return head;
ListNode *first = new ListNode(0);
first->next = head;
int cnt = 1;
for (int i = 0; i < m - 1; ++i)
{
first = first->next;
cnt++;
}
ListNode *old_pre = first;
ListNode *cur = first->next;
ListNode *cur_next = cur->next;
ListNode *new_pre = cur;
ListNode *temp;
/*反转*/
while ((cur != NULL) && (cnt < n))
{
temp = cur_next->next;
cur_next->next = cur;
cur = cur_next;
cur_next = temp;
cnt++;
}
old_pre->next = cur;
new_pre->next = cur_next;
if (m == 1)
return cur;
else
return head;
}
};
``` ```
...@@ -4,30 +4,232 @@ ...@@ -4,30 +4,232 @@
## aop ## aop
### before ### before
```cpp ```cpp
#include <bits/stdc++.h>
using namespace std;
``` ```
### after ### after
```cpp ```cpp
int main()
{
Solution sol;
vector<string> res;
string s = "25525511135";
res = sol.restoreIpAddresses(s);
for (auto i : res)
cout << i << " ";
return 0;
}
``` ```
## 答案 ## 答案
```cpp ```cpp
class Solution
{
public:
void dfs(vector<string> &res, string &s, int start, vector<string> &vec)
{
if (start == s.size())
{
if (vec.size() == 4)
{
string str;
int flag = 0;
for (int k = 0; k < 4; k++)
{
int num = atoi(vec[k].c_str());
if (num >= 0 && num <= 255)
{
str += vec[k];
}
if (k < 3)
{
str += '.';
}
}
if (flag == 0)
{
res.push_back(str);
}
}
return;
}
for (int i = start; i < s.size(); i++)
{
if (tmp != "0" && tmp.size() < 3)
{
tmp += s[i];
if (vec.size() < 4)
{
vec.push_back(tmp);
tmp = "";
dfs(res, s, i + 1, vec);
tmp = vec.back();
vec.pop_back();
}
}
}
}
vector<string> restoreIpAddresses(string s)
{
int n = s.size();
vector<string> res;
if (n < 4 || n > 12)
{
return res;
}
vector<string> vec;
dfs(res, s, 0, vec);
return res;
}
private:
string tmp;
};
``` ```
## 选项 ## 选项
### A ### A
```cpp ```cpp
class Solution
{
public:
vector<string> res;
vector<string> restoreIpAddresses(string s)
{
string temp = "";
dfs(s, temp, 0);
return res;
}
void dfs(string s, string &temp, int word_number)
{
if (word_number == 4)
{
if (s.empty())
res.push_back(temp);
}
else
{
if (word_number > 0)
temp += '.';
for (int i = 1; i <= 3 && i <= s.length(); i++)
{
if (valid(s.substr(0, i)))
{
temp += s.substr(0, i);
dfs(s.substr(i, s.length() - i), temp, word_number + 1);
temp = temp.substr(0, temp.length() - i);
}
}
temp.pop_back();
}
}
bool valid(const string &s)
{
if (s.empty() || (s[0] == '0' && s.size() > 1))
return false;
int val = stoi(s);
if (val >= 0 && val <= 255)
return true;
return false;
}
};
``` ```
### B ### B
```cpp ```cpp
class Solution
{
public:
vector<string> res;
bool isOk(int cnt, int beg, int len, string &s)
{
if (s.size() - beg < 4 - cnt || s.size() - beg > 3 * (4 - cnt))
return false;
string str = s.substr(beg, len);
if (len == 3 && stoi(str) > 255)
return false;
if (len >= 2 && str[0] == '0')
return false;
return true;
}
void backtrack(int cnt, int beg, string str, string &s)
{
if (cnt == 3)
{
if (isOk(cnt, beg, s.size() - beg, s))
res.push_back(str + s.substr(beg, s.size() - beg));
return;
}
for (int i = 1; i <= 3; ++i)
{
if (isOk(cnt, beg, i, s))
backtrack(cnt + 1, beg + i, str + s.substr(beg, i) + ".", s);
}
}
vector<string> restoreIpAddresses(string s)
{
backtrack(0, 0, "", s);
return res;
}
};
``` ```
### C ### C
```cpp ```cpp
class Solution
{
public:
vector<string> restoreIpAddresses(string s)
{
int n = s.size();
if (n > 12 || n < 4)
return vector<string>();
vector<string> res;
string ans;
for (int i = 1; i <= 3; ++i)
{
if (n - i < 3 || n - i > 9)
continue;
string s1 = s.substr(0, i);
if (i == 3 && stoi(s1) > 255)
continue;
if (i == 2 && s1[0] == '0' || i == 3 && s1[0] == '0')
continue;
for (int j = 1; j <= 3; ++j)
{
if (n - i - j < 2 || n - i - j > 6)
continue;
string s2 = s.substr(i, j);
if (j == 3 && stoi(s2) > 255)
continue;
if (j == 2 && s2[0] == '0' || j == 3 && s2[0] == '0')
continue;
for (int k = 1; k <= 3; ++k)
{
if (n - i - j - k < 1 || n - i - j - k > 3)
continue;
string s3 = s.substr(i + j, k);
if (k == 3 && stoi(s3) > 255)
continue;
if (k == 2 && s3[0] == '0' || k == 3 && s3[0] == '0')
continue;
string s4 = s.substr(i + j + k, n - i - j - k);
if (stoi(s4) > 255)
continue;
if (n - i - j - k == 2 && s4[0] == '0' || n - i - j - k == 3 && s4[0] == '0')
continue;
res.push_back(s1 + "." + s2 + "." + s3 + "." + s4);
}
}
}
return res;
}
};
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册