
全文字數: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源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。