First, calculate how many times each letter appeared in the string s, and then for each number in the
class Solution {
public:
string originalDigits(string s) {
vector<string> words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
vector<int> nums = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
vector<int> distinct = {'z', 'o', 'w', 'h', 'u', 'f', 'x', 's', 'g', 'i'};
vector<int> lcounts(26, 0);
vector<int> dcounts(10, 0);
string res = "";
for(auto c : s) lcounts[c - 'a']++;
for(auto num : nums){
dcounts[num] = lcounts[distinct[num] - 'a'];
for(char c : words[num])
lcounts[c - 'a'] -= dcounts[num];
}
for(int i = 0; i < 10; i++)
for(int j = 0; j < dcounts[i]; j++)
res += to_string(i);
return res;
}
};
从英文还原原始数字

首先,统计字符串s中每个字母出现的次数,然后为nums中的每个数字记录数字计数。例如,对于数字0,dcounts[0] = lcounts[distinct[0] - ‘a’],这表示在字符串中'z'出现了多少次,即“zero”的数量。接着,对于“zero”中的每个字母,减少lcounts的计数,因为这些字母将用于重构数字。
1455

被折叠的 条评论
为什么被折叠?



