给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9。按升序输出原始的数字。
注意:
    输入只包含小写英文字母。
    输入保证合法并可以转换为原始的数字,这意味着像 "abc" 或 "zerone" 的输入是不允许的。
    输入字符串的长度小于 50,000。
示例 1:
输入: "owoztneoer"
输出: "012" (zeroonetwo)
示例 2:
输入: "fviefuro"
输出: "45" (fourfive)
详见:https://leetcode.com/problems/reconstruct-original-digits-from-english/description/

C++:

方法一:

class Solution {
public:
string originalDigits(string s) {
string res = "";
vector<int> counts(128, 0), nums(10, 0);
for (char c : s)
{
++counts[c];
}
nums[0] = counts['z'];
nums[2] = counts['w'];
nums[4] = counts['u'];
nums[6] = counts['x'];
nums[8] = counts['g'];
nums[1] = counts['o'] - nums[0] - nums[2] - nums[4];
nums[3] = counts['h'] - nums[8];
nums[5] = counts['f'] - nums[4];
nums[7] = counts['s'] - nums[6];
nums[9] = counts['i'] - nums[6] - nums[8] - nums[5];
for (int i = 0; i < nums.size(); ++i)
{
for (int j = 0; j < nums[i]; ++j)
{
res += (i + '0');
}
}
return res;
}
};

方法二:

class Solution {
public:
string originalDigits(string s) {
string res = "";
vector<string> words{"zero", "two", "four", "six", "eight", "one", "three", "five", "seven", "nine"};
vector<int> nums{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}, counts(26, 0);
vector<char> chars{'z', 'w', 'u', 'x', 'g', 'o', 'h', 'f', 's', 'i'};
for (char c : s)
{
++counts[c - 'a'];
}
for (int i = 0; i < 10; ++i)
{
int cnt = counts[chars[i] - 'a'];
for (int j = 0; j < words[i].size(); ++j)
{
counts[words[i][j] - 'a'] -= cnt;
}
while (cnt--)
{
res += (nums[i] + '0');
}
}
sort(res.begin(), res.end());
return res;
}
};

参考:https://www.cnblogs.com/grandyang/p/5996239.html

423 Reconstruct Original Digits from English 从英文中重建数字的更多相关文章

  1. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  2. 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)

    [LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...

  3. 423. Reconstruct Original Digits from English(Medium)

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  4. [LeetCode] 423 Reconstruct Original Digits from English

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  5. LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  6. 【LeetCode】423. Reconstruct Original Digits from English

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  7. 423. Reconstruct Original Digits from English (leetcode)

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  8. 423. Reconstruct Original Digits from English

    这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...

  9. Java实现 LeetCode 423 从英文中重建数字

    423. 从英文中重建数字 给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字. 注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 ...

随机推荐

  1. 以Java属性文件的格式创建Hibernate的配置文件和DTD特殊符号作用

    演示样例代码 hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.my ...

  2. javascript/jquery模板引擎——Handlebars初体验

    Handlebars.js下载地址:http://handlebarsjs.com/ 最近自己在建一个站,采用完全的前后端分离的方式,现在正在做前端的部分.其中有项功能是需要ajax调用后端接口,返回 ...

  3. JMeter 系列之—-01使用

    用Jmeter 做压测,总体与LoadRunner 类似: 一.线程组 1. 线程数 2. 循环次数 单个线程循环次数 3. Ramp-up Period(in seconds) [1]决定多长时间启 ...

  4. 【转】TestNG执行顺序控制

    1.class执行顺序控制---testng.xml之preserve-order preserve-order:用来控制<test>里面所有<classes>的执行顺序.&l ...

  5. 小胖说事35-----Terminating app due to uncaught exception &#39;CALayerInvalidGeometry&#39;, reason: &#39;CALayer posi

    2011-06-11 15:19:17.167 ***[930:707] *** Terminating app due to uncaught exception 'CALayerInvalidGe ...

  6. wsdl2objc定制(一)namespace

    1.问题抛出: 如今还是有非常多人使用 wsdl2objc 来调用webservice,可是有时候会有不开心的事情发生, <soap:Envelope xmlns:soap="http ...

  7. 安装PyQt5和Eric6

    安装官方的指引,安装起来本来是非常简单的,但是我前后折腾了两天,甚至连Eric得源码都去调试都没成功.过程如下: 在PyQt5的官网链接中下载轮子 PyQt5-5.7.1-5.7.1-cp34.cp3 ...

  8. GCC 编译详解 (转)

    GNU CC(简称为Gcc)是GNU项目中符合ANSI C标准的编译系统,能够编译用C.C++和Object C等语言编写的程序.Gcc不仅功能强大,而且可以编译如C.C++.Object C.Jav ...

  9. 初探active mq

    mq(message queue),即消息队列,目前比较流行消息队列是active mq 和kafka.本文介绍如何简单的使用active mq. ActiveMQ官网下载地址:http://acti ...

  10. Eclipse+Maven(webapp)+Jetty+JReBel的配置方法

    maven配置 省略 jrebel配置 jrebel毋须繁琐的配置,把jrebel-5.6.3-crack.zip解压放在磁盘文件夹就可以.(笔者路径为:D:\coding-life\IDE\jreb ...