Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
generalized-abbreviation.cpp 897 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2015-12-23 09:09 +08:00 . Update generalized-abbreviation.cpp
// Time: O(n * 2^n)
// Space: O(n)
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
string cur;
generateAbbreviationsHelper(word, 0, &cur, &res);
return res;
}
void generateAbbreviationsHelper(const string& word, int i, string *cur, vector<string> *res) {
if (i == word.length()) {
res->emplace_back(*cur);
return;
}
cur->push_back(word[i]);
generateAbbreviationsHelper(word, i + 1, cur, res);
cur->pop_back();
if (cur->empty() || not isdigit(cur->back())) {
for (int l = 1; i + l <= word.length(); ++l) {
cur->append(to_string(l));
generateAbbreviationsHelper(word, i + l, cur, res);
cur->resize(cur->length() - to_string(l).length());
}
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助