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 ...
随机推荐
- HDU 4753 Fishhead’s Little Game(DFS)
题目链接 很繁琐的爆搜,最多要加2^12条边,暴力就可以,回溯那部分一直没有回溯好,写了一晚上...代码非常,非常难看..对了,还不是普通的爆搜,双向搜索博弈,以前记得看过,这次好像第一次写.. #i ...
- Lable得到自定义高度!
方法1(系统):CGSize declabesize = [_questionDecLabel.text boundingRectWithSize:CGSizeMake(CGRectGetWidth( ...
- DataGridView 中添加CheckBox和常用处理方式 .
DataGridView 中添加CheckBox和常用处理方式 文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934 DataG ...
- 将bootstrap弹出框的点击弹出改为鼠标移入弹出
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Address already in use:JVM_Bind
1.原因:端口被占用 2.解决方式: 方式一:重启电脑 方式二:方式一不行执行方式二 双击Tomcat server 将Ports下HTTP/1.1对应的Port Number 改为其他值 备注: ...
- 禁止浏览器直接访问php文件
框架中很多php文件并不是用来如果熟悉框架的路径就能直接访问这个文件,虽然访问到了也不能看到什么(他看到的是编译过后的html文件),但还是觉的很不安全. 可以通过下面这种方法拒绝他人的从浏览器中的访 ...
- 转 状态压缩DP
引入 首先来说说“状态压缩动态规划”这个名称,顾名思义,状态压缩动态规划这个算法包括两个特点,第一是“状态压缩”,第二是“动态规划”. 状态压缩: 从状态压缩的特点来看,这个算法适用的题目符合以下的条 ...
- cursor 手型样式
cursor:hand 与 cursor:pointer 的效果是一样,都像手形光标.但用FireFox浏览时才注意到使用cursor:hand在FireFox里并被支持.cursor:hand :I ...
- sql语句的各种模糊查询
一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1.%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况 ...
- 应该掌握的MySQL命令、MySQL语句
一.MySQL常用的命令: 1. 连接数据库:mysql>mysql -uroot -p回车,再输入密码 mysql -h 192.168.0.200 -P 3306 -u root -p2 ...