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, ...
随机推荐
- Flash中的注册点和中心点
用一句话概括注册点和中心点的作用,那就是:注册点用来定位,中心点用来变形 当然,这句话不是非常准确,只是暂时先这么理解,下面会详细讲解. 认识注册点 每个元件都有一个注册点.在元件编辑窗口.或在舞台中 ...
- asp.net的Server.MapPath方法
Server.MapPath()的功能: 返回与 Web 服务器上的指定虚拟路径相对应的物理文件路径. 命名空间: System.Web 程序集: System.Web(在 System.Web.dl ...
- 595. Big Countries
There is a table World +-----------------+------------+------------+--------------+---------------+ ...
- Introducing the Filter Types
The ActionFilterAttribute class implements both the IActionFilter and IResultFilter interfaces. This ...
- 02 java 基础:java 文件名与类名关系 CLASSPATH
java 类修饰符:通常情况下使用 public 修饰,此时,java 强制要求 .java 文件名需与该 public 修饰类名一致,否则无法编译通过.如若没有加修饰符,文件名与类名可无任何关联. ...
- 【转】Python高级特性——切片(Slice)
摘录廖雪峰网站 定义一个list: 1 L = ['haha','xixi','hehe','heihei','gaga'] 取其前三个元素: >>> L[0],L[1],L[2] ...
- python数据库编程_sqlite
原文请看:http://blog.csdn.net/jj_liuxin/article/details/3584448 sqlite是一个轻量级的数据库,与很多大型的数据库(例如DB2,Oracle, ...
- 树形DP题目集合
[树形DP](https://cn.vjudge.net/contest/123963#overview) #include<cstdio> #include<string> ...
- Codeforces Round #423 A Restaurant Tables(模拟)
A. Restaurant Tables time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 洛谷P4609 [FJOI2016]建筑师(第一类斯特林数+组合数)
题面 洛谷 题解 (图片来源于网络,侵删) 以最高的柱子\(n\)为分界线,我们将左边的一个柱子和它右边的省略号看作一个圆排列,右边的一个柱子和它左边的省略号看作一个圆排列,于是,除了中间的最高的柱子 ...