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 ...
随机推荐
- 【Eclipse】几个最重要的快捷键
1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/) 快速修正:Ctrl+1 单词补全:Alt+/ 打开外部Java文档:Shift+F2 显示搜索对话框:C ...
- 配置安装CocoPods后进行 项目基本配置
配置安装CocoPods后进行 项目基本配置总结 1)终端在文件根目录下输入 $ touch Podfile 创建一个空白的Podfile文件 2)然后在使用编辑器打开Podfile文件进行需要配置的 ...
- 第1章 ZigBee协议栈初始化网络启动流程
作者:宋老师,华清远见嵌入式学院讲师. ZigBee的基本流程:由协调器的组网(创建PAN ID),终端设备和路由设备发现网络以及加入网络. 基本流程:main()->osal_init_sys ...
- C#中的接口
C#中的接口(转) 转自:http://www.cnblogs.com/zhenyulu/articles/377705.html 本文中所有图示纯为个人理解(参考了Assembly中元数据的存储方式 ...
- Super Jumping! Jumping! Jumping!——E
E. Super Jumping! Jumping! Jumping! Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO forma ...
- OpenERP在哪储存附件?
我们知道对OpenERP中的每个内部对象(比如:业务伙伴,采购订单,销售订单,发货单,等等)我们都可以添加任意的附件,如图片,文档,视频等.那么这些附件在OpenERP内部是如何管理的呢? 默认情况下 ...
- 使用explain查看mysql查询执行计划
explain语句: 字段解释: type: all(全表扫描) ref() possible_keys: 预测使用什么列做为索引 key: 实际使用的key ...
- getElementsByClassName的兼容性
/*----------------------------index.html------------------------------------*/ <!DOCTYPE html> ...
- oracle中复制表和数据 && 多表插入语句
创建测试表和测试数据 create table test (id number,name varchar(10)); insert into test values(1,'liufang'); ...
- Tree树形结构案例+源码
上次用到Tree结构,自己不知道写,就在网上找了几个相对较好的tree小案例,留着自己用,希望也能帮到你 下面是案例的一些图片,压缩文件已近上传至博客当和百度云中. 下载链接:http://pan.b ...