当前位置:网站首页>leetcode:423. Reconstructing numbers from English
leetcode:423. Reconstructing numbers from English
2022-04-21 18:58:00 【Oceanstar's study notes】
Title source
Title Description

title
We know , Each number corresponds to an English word ,“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”, If you look at each English word in these words , We can find out :
- The characters of some words are unique
- such as
z, Only inzeroin - also
w,u,x,gThese four words , They only appear intwo,four,six,eightin - Then the number of these five numbers can be determined
- such as
- Some words appear many times
- Because of the
oThe words arezero,two,four,one, The first three of them have been identified , thatoneYou will know the number of - Because of the
hThe words areeight,three, amongeightThe number is known , thatthreeJust know the number of - Because of the
fThe words arefour,five, amongfourThe number is known , thatfiveJust know the number of ; - Because of the
sThe words aresix,seven, amongsixThe number is known , thatsevenJust know the number of ; - Because of the
iThe words aresix,eight,five,nine, The first three of them have been identified , thatnineJust know the number of
- Because of the
Ideas : First count the number of times each character appears , Then calculate the number of times each word appears , Then you can rebuild .
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;
}
};
class Solution {
public:
string originalDigits(string s) {
std::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 < 10; ++i) {
for (int j = 0; j < nums[i]; ++j) {
// There are several 1, There are several 2, There are several 3
res += (i + '0');
}
}
return res;
}
};

版权声明
本文为[Oceanstar's study notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211853086861.html
边栏推荐
- 【大话云原生】负载均衡篇-小饭馆客流量变大了
- Log4j日志的用法
- How much can I get a month with a PMP certificate
- VB judges whether a number is a prime number.
- OBS access network camera
- EasyGBS关闭了录像计划,为何还有录像文件生成?
- How does Tianchuang Suying charge for static creativity?
- 数据库进阶学习:索引介绍及Btree,B+tree,hash
- 【深度之眼】情感分析——循环神经网络用于多任务学习的文本分类TextRNN
- SSH keygen setting password free login
猜你喜欢

面试官问我谈谈对事务隔离机制的理解?我是这样回答的

低密度脂蛋白研究丨LDL-C含量检测试剂盒方案

What kind of Bluetooth headset is inexpensive and practical? Wireless Bluetooth headset for student party

Crystal Chem小鼠葡萄糖检测试剂盒说明书

2022.04.21(LC_452_用最少数量的箭引爆气球)

How does PR open MKV files? How can MKV files be converted to MP4 and how can MP4 be consumed by ImageJ?

Abbexa 山羊 F(ab‘)2 IgG 同种型对照

Abbexa 驴抗山羊 IgG (H&L) 抗体

【js学习笔记四十】复杂工厂模式

【大话云原生】负载均衡篇-小饭馆客流量变大了
随机推荐
每日一题系列:洗牌
Tensorflow from foundation to practice 00 tensor, conversation, variable, matrix
数据库进阶学习:索引分类和创建语法
669. 修剪二叉搜索树
重大活动评论狂欢活动链路建设
干货 | 面试过了,如何谈薪资?拿高薪Offer有哪些技巧?
《实战》 用Tensorflow 实现线性回归
The video course "vite Learning Guide - webify deployment project based on Tencent cloud" was launched "cloud + community"
常见Shell命令 - grep(一)
Niu Ke - alternative addition
每日一题系列:汽水瓶
刚毕业大学生考PMP用处大吗?
Leetcode 824.山羊拉丁文
排序会了递归,不学非递归太可惜了
CVPR2022 Oral | CosFace、ArcFace的大统一升级,AdaFace解决低质量图像人脸识
【js学习笔记四十一】单体模式
[untitled]
面试官问我谈谈对事务隔离机制的理解?我是这样回答的
Apache Doris创建动态分区
电网GPS北斗卫星时间同步系统(时钟装置)技术介绍