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.按升序输出原始的数字. 注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 ...
随机推荐
- Win7 Windows Update更新的文件默认在哪个位置
C:\Windows\SoftwareDistribution\download
- centos中w使用smbclient连接window出现:session setup failed: NT_STATUS_LOGON_FAILURE
1. 在window中网络->我自己的电脑->能够查看到共享文件,说明window的共享是正常了; 2. 在window中配置共享时,使用的是仅仅同意超级管理员訪问,可是我把超级管理员改名 ...
- hive计算网页停留时长
hive表结构例如以下: create table pv_user_info( session_id string, user_id string, url string, starttime big ...
- ReLu(修正线性单元)、sigmoid和tahh的比较
不多说,直接上干货! 最近,在看论文,提及到这个修正线性单元(Rectified linear unit,ReLU). Deep Sparse Rectifier Neural Networks Re ...
- Django初识二
1,在django中用于提交的form表单中的三要素: 1.1>form标签要有action和method,上传文件需要额外指定的enctype 1.2>获取用户输入的标签要有name属性 ...
- grails Domian对象转JSON去class以及自己定义字段的最佳方式
grails:2.4.x IDE:Intellij IDEA 13.x grails的Domain对象之间假设存在环形引用.直接使用as JSON仅仅会输出关联对象的id.而且假设使用deep也会报错 ...
- YTU 1004: 1、2、3、4、5...
1004: 1.2.3.4.5... 时间限制: 1000 Sec 内存限制: 64 MB 提交: 1275 解决: 343 题目描述 浙江工商大学校园里绿树成荫,环境非常舒适,因此也引来一批动物 ...
- windows系统下mysql5.5查看和设置数据库编码
1.显示当前编码命令: show variables like 'char%'; 2.设置编码为utf8命令:set names 'utf8';
- INT_PTR数据类型
A signed integer type for pointer precision. Use when casting a pointer to an integer to perform poi ...
- 关于netty的简单实现
1. 新建两个maven项目, 分别为 netty-server: netty的服务端: 消息的消费者 netty-client: netty的客户端: 消息的生产者 2. 分别引入netty的mav ...