Leetcode: 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"
# of '0': # of 'z'
# of '2': # of 'w'
4: u
6: x
8: g
3: h - 8
5: f - 4
7: s - 6
1: o - 0 - 2 - 4
9: i - 5 - 6 - 8
public String originalDigits(String s) {
int[] count = new int[10];
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if (c == 'z') count[0]++;
if (c == 'w') count[2]++;
if (c == 'x') count[6]++;
if (c == 's') count[7]++; //7-6
if (c == 'g') count[8]++;
if (c == 'u') count[4]++;
if (c == 'f') count[5]++; //5-4
if (c == 'h') count[3]++; //3-8
if (c == 'i') count[9]++; //9-8-5-6
if (c == 'o') count[1]++; //1-0-2-4
}
count[7] -= count[6];
count[5] -= count[4];
count[3] -= count[8];
count[9] = count[9] - count[8] - count[5] - count[6];
count[1] = count[1] - count[0] - count[2] - count[4];
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 9; i++){
for (int j = 0; j < count[i]; j++){
sb.append(i);
}
}
return sb.toString();
}
我的code用了一个数组来存char count
public class Solution {
public String originalDigits(String s) {
StringBuilder res = new StringBuilder();
if (s==null || s.length()==0) return "";
int[] chars = new int[26];
int[] digits = new int[10];
for (int i=0; i<s.length(); i++) {
chars[s.charAt(i)-'a']++;
}
count(chars, digits);
for (int i=0; i<digits.length; i++) {
for (int j=0; j<digits[i]; j++) {
res.append(i);
}
}
return res.toString();
}
public void count(int[] chars, int[] digits) {
//'0'
digits[0] = chars['z'-'a'];
//'2'
digits[2] = chars['w'-'a'];
//'4'
digits[4] = chars['u'-'a'];
//'6'
digits[6] = chars['x'-'a'];
//'8'
digits[8] = chars['g'-'a'];
//'1' and '2' and '0' and '4' share 'o'
digits[1] = chars['o'-'a'] - digits[2] - digits[0] - digits[4];
//'3' and '8' share 'h'
digits[3] = chars['h'-'a'] - digits[8];
//'5' and '4' share 'f'
digits[5] = chars['f'-'a'] - digits[4];
//'7' and '6' share 's'
digits[7] = chars['s'-'a'] - digits[6];
//'9' and '5' and '6' and '8' share 'i'
digits[9] = chars['i'-'a'] - digits[5] - digits[6] - digits[8];
}
}
Leetcode: 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 ...
- [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 ...
- [Swift]LeetCode423. 从英文中重建数字 | 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(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 ...
随机推荐
- ajax操作时用于提高用户体验的两段备用代码
<div id="msgBoxDIV" style="position: absolute; width: 50%; padding-top: 2px; heigh ...
- IE6、7绝对定位层被遮挡的原因(主要是父层决定的)
最近做项目,经常遇到IE7以下浏览器中.一些悬浮框被一些元素遮挡的问题,这些元素一般都是设置了position的.问题的根本在不是被设置绝对定位的元素上,而是在设置了相对定位的父元素上. 我查阅了 ...
- apache中.htaccess不起作用
找到apache的配置文件httpd.conf文件,找到: 代码如下 复制代码 #LoadModule rewrite_module modules/mod_rewrite.so 去掉前面的#号. ...
- HDU1568
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- html5学习小结,float练习。
经过两天的H5学习之后,做了一下float属性的练习,要做出来的效果为: 下面为代码部分,所用到的知识不多,不过才现在刚开始,以后要学的东西还有很多,大家继续加油! <!DOCTYPE html ...
- Python开发问题和解决方案汇集
1.Sublime Text中用Tab批量替换空格Whitespace缩进:Ctrl+A全选代码,Ctrl+Shift+P打开下拉框,输入indent,找到Convert indentation to ...
- Rearrange a string so that all same characters become d distance away
Given a string and a positive integer d. Some characters may be repeated in the given string. Rearra ...
- ping & tracert over TCP
偶然发现还有这样的工具: 通过TCP协议实现ping和tracert. 之前一直苦恼无法通过ping的方式测试被q网站, 现在有了这两个工具后就方便了. [Windows] tcping: http: ...
- xx.substring(x,x)和xx.index()
[转的]用一个例子解释: ip = "126.168.1.1"; i = ip.indexOf('.'); 这里默认从0开始找到 ...
- IIS应用程序池最大进程数设置
1.当工作进程数>1时,如果有多个连接请求就会启动多个工作进程实例来处理,所启动的最多工作进程数就是你设置的最大进程数,后续更多的连接请求会循环的发送至不同的工作进程来处理.每个工作进程都能承担 ...