首页 > 学院 > 开发设计 > 正文

93. Restore IP Addresses

2019-11-10 21:51:56
字体:
来源:转载
供稿:网友

无敌代码暴力。。。。。,2刷还是乖乖dfs吧

class Solution {public: vector<string> restoreipAddresses(string s) { vector<string> ret; string ans; for (int a=1; a<=3; a++) for (int b=1; b<=3; b++) for (int c=1; c<=3; c++) for (int d=1; d<=3; d++) if (a+b+c+d == s.length()) { 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) if ( (ans=to_string(A)+"."+to_string(B)+"."+to_string(C)+"."+to_string(D)).length() == s.length()+3) ret.push_back(ans); } return ret; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表