reconstruct-original-digits-from-english(好)
https://leetcode.com/problems/reconstruct-original-digits-from-english/ //https://discuss.leetcode.com/topic/63386/one-pass-o-n-java-solution-simple-and-clear public class Solution {
// zero one two three four five six seven eight nine ten
// z 0
// e 0 1 3 3 5 7 7 8 9
// r 0 3 4
// o 0 1 2 4
// n 1 7 9 9
// t 2 3 8
// w 2
// h 3 8
// f 4 5
// u 4
// i 5 6 8 9
// v 5 7
// s 6 7
// x 6
// g 8 public String originalDigits(String s) {
// 太牛了,开始我也想到统计,但是想到删除字符串那些复杂的操作上面去了 int[] counts = new int[10];
for (int i=0; i<s.length(); i++) {
char ch = s.charAt(i);
if (ch == 'z') counts[0]++;
if (ch == 'u') counts[4]++;
if (ch == 'w') counts[2]++;
if (ch == 'x') counts[6]++;
if (ch == 'g') counts[8]++;
if (ch == 'h') counts[3]++; // 3, 8
if (ch == 's') counts[7]++; // 6, 7
if (ch == 'f') counts[5]++; // 4, 5
if (ch == 'o') counts[1]++; // 0, 1, 2, 4
if (ch == 'i') counts[9]++; // 5, 6, 8, 9
} counts[3] -= counts[8];
counts[7] -= counts[6];
counts[5] -= counts[4];
counts[1] -= counts[0] + counts[2] + counts[4];
counts[9] -= counts[5] + counts[6] + counts[8]; StringBuilder sb = new StringBuilder();
for (int i=0; i<10; i++) {
for (int j=0; j<counts[i]; j++) {
sb.append(i);
}
}
return sb.toString();
}
}
reconstruct-original-digits-from-english(好)的更多相关文章
- 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- [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
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 ...
- 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
这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...
随机推荐
- Nginx惊群处理
惊群:是指在多线程/多进程中,当有一个客户端发生链接请求时,多线程/多进程都被唤醒,然后只仅仅有一个进程/线程处理成功,其他进程/线程还是回到睡眠状态,这种现象就是惊群. 惊群是经常发生现在serve ...
- mac安装jdk1.8
一. http://www.oracle.com/technetwork/java/javase/downloads/index.html 去jdk官网下载 二.安装 一路傻瓜式安装,下一步下一步 三 ...
- hdu多校6
这个场要恶心死我了.. 1001 积分题,不要四舍五入 //#pragma comment(linker, "/stack:200000000") //#pragma GCC op ...
- 关于xargs cp中,如何确定拷贝的源和目的
来源: http://bbs.chinaunix.net/thread-1022095-1-1.html Seker: find . -name "*" |xargs cp ??? ...
- 跨域请求httpclient
httpclient:是Apache工具包,util,它可以作为一个爬虫,直接爬取某个互联网上的页面.获取到时页面最终的源文件html.直接可以获取页面返回json.就可以直接在代码内部模拟发起htt ...
- Linux搭建主从数据库服务器(主从复制)
配置主机数据库: 1.克隆linux操作系统 2.修改Linux系统主机IP地址 主机IP:192.168.247.150 从机IP:192.168.247.151 3.通过xshell连接Maste ...
- vue v-bind绑定属性和样式
这期跟大家分享的,是v-bind指令.它可以往元素的属性中绑定数据,也可以动态地根据数据为元素绑定不同的样式. 绑定属性 最简单的例子,我们有一张图片,需要定义图片的src.我们可以直接在元素的属性里 ...
- HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 转:Apache+Fastcgi+Django
07年作者就贴出的文章了,可见多么古老的运行方式还在用. 转:http://www.cnblogs.com/RChen/archive/2007/03/23/django_fcgi.html 首先要安 ...
- 区间DP(区间最优解)题目讲解总结
1:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度. [分析]区间DP要覆盖整个区间,那么要求所有情况的并集. 先想出状态方程: dp[i][j]:i ~ j区间内最大匹配数目 输出:dp ...