其他分享
首页 > 其他分享> > 【解题报告】Leecode 423. 从英文中重建数字——Leecode每日一题系列

【解题报告】Leecode 423. 从英文中重建数字——Leecode每日一题系列

作者:互联网

今天是坚持每日一题打卡的第二十五天


题目链接:https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/


题解汇总:https://zhanglong.blog.csdn.net/article/details/121071779


题目描述

给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9)。按 升序 返回原始的数字。

示例 1:
输入:s = “owoztneoer”
输出:“012”

示例 2:
输入:s = “fviefuro”
输出:“45”

提示:
1 <= s.length <= 105
s[i] 为 [“e”,“g”,“f”,“i”,“h”,“o”,“n”,“s”,“r”,“u”,“t”,“w”,“v”,“x”,“z”] 这些字符之一
s 保证是一个符合题目要求的字符串


试着把0-9的英文写出来,发现可以用每个数字中独特的字母代替出现的次数,如zero中的z。

class Solution {
public:
    string originalDigits(string s) {
        unordered_map<char, int>count;      // 统计每个英文名数字出现的次数
        for (auto i : s) count[i]++;
        int arr[15] = {0};
        arr[0] = count['z'];                // zero,z
        arr[2] = count['w'];                // two,w
        arr[4] = count['u'];                // four,u
        arr[6] = count['x'];                // six,x
        arr[7] = count['s'];                // seven,s
        arr[8] = count['g'];                // eight,g
        arr[5] = count['f'] - arr[4];       // five,f
        arr[3] = count['t'] - arr[2] - arr[8];  // three
        arr[1] = count['o'] - arr[0] - arr[2] - arr[4];
        arr[9] = count['i'] - arr[5] - arr[6] - arr[8];
        string res;
        for (int i = 0; i < 10; ++i) {
            while (arr[i]--) res += (char)('0' + i);
        }
        return res;
    }
};

测试用例:"onetwothreefourfivesixseveneightnine"

标签:count,arr,string,示例,res,Leecode,解题,423,数字
来源: https://blog.csdn.net/weixin_43899069/article/details/121511491