|
| 1 | +/* |
| 2 | + * @lc app=leetcode.cn id=816 lang=cpp |
| 3 | + * |
| 4 | + * [816] 模糊坐标 |
| 5 | + */ |
| 6 | +#include <vector> |
| 7 | +#include <string> |
| 8 | +using namespace std; |
| 9 | +// @lc code=start |
| 10 | +class Solution { |
| 11 | +private: |
| 12 | + vector<string> rs; |
| 13 | + |
| 14 | + bool checkDigit(string s, int i) { |
| 15 | + if (s.size() != 1) { |
| 16 | + if (s[0] == '0' && i != 1) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + if (i != 0 && s[s.size() - 1] == '0') { |
| 20 | + return false; |
| 21 | + } |
| 22 | + } |
| 23 | + return true; |
| 24 | + } |
| 25 | + |
| 26 | + string makeStr(string a, string b) { |
| 27 | + return "(" + a + ", " + b + ")"; |
| 28 | + } |
| 29 | + |
| 30 | + string genStr(string a, int i) { |
| 31 | + if (i == 0) { |
| 32 | + return a; |
| 33 | + } |
| 34 | + return a.substr(0, i) + "." + a.substr(i, a.size()); |
| 35 | + } |
| 36 | + |
| 37 | + void aC(string s, int i) { |
| 38 | + if (i >= s.size()) { |
| 39 | + return; |
| 40 | + } |
| 41 | + string a = s.substr(0, i); |
| 42 | + string b = s.substr(i, s.size() - i); |
| 43 | + |
| 44 | + for(int i = 0; i < a.size(); i++) { |
| 45 | + string s1 = genStr(a, i); |
| 46 | + if (checkDigit(s1, i)) { |
| 47 | + for (size_t j = 0; j < b.size(); j++) { |
| 48 | + string s2 = genStr(b, j); |
| 49 | + if (checkDigit(s2, j)) { |
| 50 | + rs.push_back(makeStr(s1, s2)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + } |
| 56 | + aC(s, i + 1); |
| 57 | + } |
| 58 | +public: |
| 59 | + vector<string> ambiguousCoordinates(string s) { |
| 60 | + rs.clear(); |
| 61 | + aC(s.substr(1, s.size() - 2), 1); |
| 62 | + return rs; |
| 63 | + } |
| 64 | +}; |
| 65 | +// @lc code=end |
| 66 | + |
0 commit comments