Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
reconstruct-original-digits-from-english.cpp 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-10-19 00:35 +08:00 . Update reconstruct-original-digits-from-english.cpp
// Time: O(n)
// Space: O(1)
class Solution {
public:
string originalDigits(string s) {
const vector<string> numbers{"zero", "one", "two", "three",
"four", "five", "six", "seven",
"eight", "nine"};
vector<vector<int>> cnts(numbers.size(), vector<int>(26));
for (int i = 0; i < numbers.size(); ++i) {
for (const auto& c : numbers[i]) {
++cnts[i][c - 'a'];
}
}
// The order for greedy method.
vector<int> order{0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
// The unique char in the order.
vector<char> unique_chars{'z', 'o', 'w', 't', 'u', 'f', 'x', 's', 'g', 'n'};
vector<int> cnt(26);
for (const auto& c : s) {
++cnt[c - 'a'];
}
string result;
for (const auto& i : order) {
while (cnt[unique_chars[i] - 'a'] > 0) {
for (int j = 0; j < cnt.size(); ++j) {
cnt[j] -= cnts[i][j];
}
result.push_back(i + '0');
}
}
sort(result.begin(), result.end());
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

搜索帮助