class Solution { public: vector<int>v1; vector<string>v; int count; string inttostring(int n) { stringstream ss; string str; ss<<n; ss>>str; return str; } vector<string> restoreIpAddresses(string s) { v.clear(); if(s.size()>12)return v; v1.clear(); count=0; ipdivide(0,s); return v; } void ipdivide(int depth,string s) { if(depth==s.size()&&count==4) { string ipaddress=""; for(int i=0;i<4;i++) { ipaddress+=inttostring(v1[i]); if(i!=3)ipaddress+="."; } v.push_back(ipaddress); } if(depth<s.size()) { int temp=0; for(int i=depth;i<s.size();i++) { temp=temp*10+(s[i]-'0'); if(temp>255)break; else { count=count+1; v1.push_back(temp); ipdivide(i+1,s); v1.pop_back(); count--; if(temp==0)break; } } } } };
犯的两个错误:一个是s[i]-'0'写成了s[depth]-'0';一个是未对s.size()判断导致judge large超时