
全文字数:1460字html
全文阅读时间:4分钟web

【Leetcode】题目描述
算法
输入: “25525511135”
输出: [“255.255.11.135”, “255.255.111.35”]
A类地址网络号范围:1.0.0.0-126.0.0.0微信
B类地址网络号范围:128.0.0.0-191.255.0.0网络
C类地址网络号范围:192.0.0.0-223.255.255.0数据结构
D类地址范围:224.0.0.0-239.255.255.255app
E类地址范围:240.0.0.0-255.255.255.254编辑器
复原IP地址[1]学习
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> res;
for (int a = 1; a < 4; ++a)
for (int b = 1; b < 4; ++b)
for (int c = 1; c < 4; ++c)
for (int d = 1; d < 4; ++d)
if (a + b + c + d == s.size()) {
int A = stoi(s.substr(0, a));
int B = stoi(s.substr(a, b));
int C = stoi(s.substr(a + b, c));
int D = stoi(s.substr(a + b + c, d));
if (A <= 255 && B <= 255 && C <= 255 && D <= 255) {
string t = to_string(A) + "." + to_string(B) + "." + to_string(C) + "." + to_string(D);
if (t.size() == s.size() + 3) res.push_back(t);
}
}
return res;
}
};
参考资料
【Leetcode】复原IP地址: https://leetcode-cn.com/problems/restore-ip-addresses/flex
- End -
往期推荐
🔗
1
长按关注
本文分享自微信公众号 - hahaCoder(hahaCoder)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。