笛卡尔乘积问题
笛卡爾乘積問題的判別
笛卡爾問題的一個特點是有很多個種類的子結果的拼接。目前遇到的只是字符串的拼接。
通用解決思路
- 分步求解結果,最后拼接;
例題
Leetcode 816.模糊坐標
class Solution { public:vector<string> addSymbol(string s) {vector<string> res;int length = s.length();for(int i = 1; i <= length - 1; i++) {string temp = s;// 剪枝if(temp[0] == '0' && i > 1) continue;if(temp[length-1] == '0') continue;temp.insert(i, ".");res.push_back(temp);}if(s == "0" || s[0] != '0') res.push_back(s);return res;}vector<string> ambiguousCoordinates(string S) {// S去除兩邊的括號string s = S.substr(1, S.size()-2);int length = s.length();// 結果保存vector<string> res;// 遍歷逗號插入位置for(int i = 1; i <= length - 1; i++) {// 分割字符串vector<string> x = addSymbol(s.substr(0,i));vector<string> y = addSymbol(s.substr(i, length-i));// 笛卡爾乘積for(auto i : x) {for(auto j : y) {string temp = "(" + i + ", " + j + ")";res.push_back(temp);}}}return res;} };總結
- 上一篇: mysql 大小限制_关于sql:MyS
- 下一篇: 抖音直播间没人气?速看让直播间人气快速突