0816.cpp (1184B)
1 class Solution { 2 static bool valid(const string &s) { 3 const int n = size(s); 4 if (n == 1) return true; 5 if (s[0] == '0' && s[1] != '.') return false; 6 7 for (int i = 0; i < n; i++) { 8 if (s[i] == '.') return s[n - 1] != '0'; 9 } 10 11 return true; 12 } 13 14 const vector<string> devide(const string &s) const { 15 const int n = size(s); 16 vector<string> res; 17 if (valid(s)) res.push_back(s); 18 string left(1, s[0]); 19 for (int i = 1; i < n; i++) { 20 const string crnt = left + '.' + s.substr(i, n - i); 21 if (valid(crnt)) res.push_back(crnt); 22 left += s[i]; 23 } 24 return res; 25 } 26 27 public: 28 vector<string> ambiguousCoordinates(const string &s) const { 29 const int n = size(s) - 2; 30 vector<string> res; 31 string left; 32 for (int i = 1; i < n; i++) { 33 left += s[i]; 34 for (const auto &a : devide(left)) { 35 for (const auto &b : devide(s.substr(i + 1, n - i))) { 36 res.push_back("(" + a + ", " + b + ")"); 37 } 38 } 39 } 40 return res; 41 } 42 };