320. Generalized Abbreviation
做题历程:
- 2016/Oct/23 本题是第一次做,本次耗时18分钟,独立解出
本题我就是用recursive做的,核心就是当求得"rod"的所有可能性之后,可以在前面附加"w"或者数字"1",当然数字"1"可能与"ord"自带的数字合并。。
当然,这道题我的写法太耗内存了,不过因为是第一次做,就先将就了,代码如下:
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> new_res;
if (word.empty()) {
new_res.push_back("");
return new_res;
}
vector<string> res = generateAbbreviations(word.substr(1));
for (int i = 0; i < res.size(); ++i) {
new_res.push_back(word[0] + res[i]);
int num = 0;
int j = 0;
while (j < res[i].size() && isdigit(res[i][j])) {
num = num * 10 + (res[i][j++] - '0');
}
new_res.push_back(to_string(num + 1) + res[i].substr(j));
}
return new_res;
}
};