[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 digits in ascending order.
Note:
- Input contains only lowercase English letters.
- Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
- Input length is less than 50,000.
Example 1:
Input: "owoztneoer" Output: "012"
Example 2:
Input: "fviefuro" Output: "45"
解法:原来的字符串是乱序的,当遇到一个字母 e 的时候,并没有办法知道这个字符是属于 one、three、nine等中的一个。但是当遇到一个 z 的时候,肯定
是可以确定这个字符是属于 zero 的,因为z只出现在zero中,同理,x 也只出现在six中。
那么这就是一个优先级的问题,应该优先匹配那些属于少数数字的字符,例如优先寻找z,如果出现一个z,那么必须相应的出现e、r、o。如果字符串里面已经没有z了,
那么这个时候如果还出现了r,那么这个 r 就只可能属于 three、four中的一个了,依次类推,就能得到解法。
for (char c : s.toCharArray()) {
count[c]++;
}
int[] res = new int[10];
char[] idx = {'z', 'x', 's', 'v', 'f', 'r', 'w', 'g', 'o', 'i'};
//char[] idx = {'z', 'x', 's', 'v', 'f', 'r', 'w', 'g', 'o', 'i'};
for (char i : idx) {
while (count[i] > 0) {
switch (i) {
case 'z': //匹配z,减去zero
res[0]++;
count['z']--;
count['e']--;
count['r']--;
count['o']--;
break;
case 'x': //匹配six,减去six
res[6]++;
count['s']--;
count['i']--;
count['x']--;
break;
case 's': //匹配seven,因为之前优先匹配了six,如果这个时候,还有多余的s,那么肯定是seven
res[7]++;
count['s']--;
count['e']--;
count['v']--;
count['e']--;
count['n']--;
break;
case 'v': //匹配five,因为之前优先匹配了seven,如果这个时候还出现v,那么肯定是属于five
res[5]++;
count['f']--;
count['i']--;
count['v']--;
count['e']--;
break;
case 'f': //匹配four,因为之前匹配了four,那么这个时候还出现f,那么肯定属于four
res[4]++;
count['f']--;
count['o']--;
count['u']--;
count['r']--;
break;
case 'r': //匹配three,因为之前匹配了four,还出现three,肯定属于three
res[3]++;
count['t']--;
count['h']--;
count['r']--;
count['e']--;
count['e']--;
break;
case 'w':
res[2]++;
count['t']--;
count['w']--;
count['o']--;
break;
case 'g':
res[8]++;
count['e']--;
count['i']--;
count['g']--;
count['h']--;
count['t']--;
break;
case 'o':
res[1]++;
count['o']--;
count['n']--;
count['e']--;
break;
case 'i':
res[9]++;
count['n']--;
count['i']--;
count['n']--;
count['e']--;
break;
default:
break;
}
}
}
//System.out.print(res[0]);
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 9; i++) {
for (int j = 0; j < res[i]; j++) {
sb.append(i);
}
}
return sb.toString();
}
[LeetCode] 423 Reconstruct Original Digits from English的更多相关文章
- 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 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- 【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(Medium)
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- 423 Reconstruct Original Digits from English 从英文中重建数字
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字.注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 "ab ...
- 423. Reconstruct Original Digits from English
这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...
- [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: Reconstruct Original Digits from English
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
随机推荐
- Android遍历获取Office格式(Word,Excel,PPT,PDF)的文件并打开
此案例主要是模仿QQ加载WPS(Word,Excel,PPT)本地文件可打开查看,使用ListView加载,使用线程扫描SD卡下所有目录加载指定的Word,Excel,PPT等格式的文件,ListVi ...
- IE6/IE7/IE8兼容H5标签
可以使用html5shiv(html5shiv主要解决HTML5提出的新元素不被IE6-8识别,这些新元素不能作为父节点包裹子元素,并且不能应用CSS样式)来解决 <!--[if lt IE 9 ...
- sqlalchemy 大全
SQLAchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行S ...
- python实验一:画图
题目:画图,学用rectangle画方形. rectangle(int left, int top, int right, int bottom) 参数说明:(left ,top )为矩形的左上坐标, ...
- hdu 2586 How far away ?(离线求最近公共祖先)
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i ...
- python迭代器和生成器
迭代器 #可以被netxt()函数调用不断返回一个值的对象成为迭代器:Iterator #迭代器是访问集合元素的一种方式,从集合第一个元素开始(用next()方法)访问就不能回退,便于循环遍历一些较大 ...
- IIS 7 的 500 內部錯誤
印象中這個有名的錯誤提示從 ASP 時代就已經存在 不是很明白微軟怎麼想的 你們所面對的客戶族群有為數不少的開發人員 除錯環境對開發者來說算是基本配備 為何不把這些相關設定做成「出廠預設值」? 都幾年 ...
- 各大门户网站的css初始化代码
腾讯QQ官网 css样式初始 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select ...
- 搭建高可用mongodb集群(三)—— 深入副本集内部机制
在上一篇文章<搭建高可用mongodb集群(二)—— 副本集> 介绍了副本集的配置,这篇文章深入研究一下副本集的内部机制.还是带着副本集的问题来看吧! 副本集故障转移,主节点是如何选举的? ...
- 使用phar上线你的代码包
在我前一阵子写的一篇文章<新版 SegmentFault 重构之系统架构>中,很多人对其中提到的利用phar上线代码比较感兴趣,我就在这边跟大家分享下我目前的做法. 哪些项目适合phar打 ...