Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ambiguous-coordinates.cpp 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-04-16 00:02 +08:00 . Update ambiguous-coordinates.cpp
// Time: O(n^4)
// Space: O(n^2)
class Solution {
public:
vector<string> ambiguousCoordinates(string S) {
vector<string> result;
for (int i = 1; i < S.length() - 2; ++i) {
const auto& lefts = make(S, 1, i);
const auto& rights = make(S, i + 1, S.length() - 2 - i);
for (const auto& left : lefts) {
for (const auto& right : rights) {
string s;
s += "(";
s += left;
s += ", ";
s += right;
s += ")";
result.emplace_back(move(s));
}
}
}
return result;
}
private:
// Time: O(n^2)
// Space: O(n^2)
vector<string> make(const string& S, int i, int n) {
vector<string> result;
for (int d = 1; d <= n; ++d) {
const auto& left = S.substr(i, d);
const auto& right = S.substr(i + d, n - d);
if ((left.front() != '0' || left == "0") &&
right.back() != '0') {
string s;
s += left;
s += (!right.empty() ? "." : "");
s += right;
result.emplace_back(move(s));
}
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助