Reconstruct Original Digits from English

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

First, calculate how many times each letter appeared in the string s , and then for each number in the nums record digit counts. For example, for number 0, dcounts[0] = lcounts[distinct[0] - ‘a’], which represents how many times ‘z’ appeared in the string; in other words, how many “zero” in the string. Next for every letter in “zero”, reduce the times from lcounts , because these letters will be used in reconstructing the digits.

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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值