423 Reconstruct Original Digits from English 从英文中重建数字
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字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 从英文中重建数字的更多相关文章
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- 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 ...
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- 423. Reconstruct Original Digits from English
这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...
- Java实现 LeetCode 423 从英文中重建数字
423. 从英文中重建数字 给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字. 注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 ...
随机推荐
- poj 1426 Find The Multiple ( BFS+同余模定理)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18390 Accepted: 744 ...
- 一个bug在redmine中的诞生到终结
1.測试员測试出bug,跟踪状态为支持,状态为新建,指派给产品经理. 2.产品经理鉴定确觉得bug.改动跟踪状态为bug.指派给技术经理: 3.技术经理收到bug,指派给开发者: 4.开发者收到bug ...
- soapUI系列之—-04 问题解决 获取接口返回报文response报错
1. SoapUI+Groovy中"org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA" 通过So ...
- JavaSE入门学习23:Java面向对象之构造方法
学了JavaSE面向对象这一部分,也该对构造方法做一个总结了. 一构造方法 在多数情况下,初始化一个对象的终于步骤是去调用这个对象的构造方法. 构造方法负责对象的初始化工作,为 实例变量赋予合适的初始 ...
- JS表格分页组件:fupage的设计思路和详细使用方法(未来考虑开源,争取在2015年)
一.背景 之前在秒针工作的时候,某js高级project师写了非常多自己的组件.当中一套是分页组件.叫做st-grid. 只是在我看来,bug太多.我常常给他反馈bug,我也不清楚为啥 ...
- PRD编写Axure内直接编辑
流程&页面&交互&逻辑 功能点: 1,选项类 设置默认值. 2,输入文本类 设置最多最少字符数. 3,功能按钮,如提交.发布. 判断敏感词,如果有,则点击发布的时候,悬浮提醒“ ...
- 反混淆、反编译unity3d动画插件DFTweenLite得到源代码
出处:http://blog.csdn.net/u010019717 author:孙广东 时间:2015.3.17 23:00 我为什么要得到这个源代码.由于有洁癖! 对于Itween ...
- js可视区域图片懒加载
可视区域图片懒加载 实现原理,页面滚动时获取需要懒加载的图片,判断此图片是否在可视区域内,是则设置图片data-src地址为src地址,加载图片. html下载地址 <!DOCTYPE html ...
- Android图片载入缓存框架Glide
Glide开源框架是Google推荐的图片载入和缓框架,其在Github上的开源地址是:https://github.com/bumptech/glide 当然一个Google推荐的框架肯定就是Vol ...
- MVC Web Api 发布到Azure报错
I fixed this by reinstalling the NuGet package, which corrects broken dependencies. From the package ...