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, ...
随机推荐
- HDU-4255
A Famous Grid Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)
1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...
- ES6新数据结构Set让数组去重
function unique(array){ return Array.from(new Set(array)); } var arr = ['aa','bb','cc','',1,0,'1',1, ...
- AC日记——「SDOI2017」序列计数 LibreOJ 2002
「SDOI2017」序列计数 思路: 矩阵快速幂: 代码: #include <bits/stdc++.h> using namespace std; #define mod 201704 ...
- springBoot service层 事务控制
springBoot使用事物比较简单,在Application启动类s上添加@EnableTransactionManagement注解,然后在service层的方法上添加@Transactional ...
- HP自动检查html标签是否闭合
function HtmlClose($body) { $strlen_var = strlen($body); // 不包含 html 标签 if (strpos($body, '<') == ...
- phpstorm中Xdebug的使用
目 录 1.Xdebug简介 2.Xdebug的安装.操作 2.1环境搭建 2.2配置php.ini 2.3配置PhpStorm 2.4配置PHP Debug 2.5进行调试 1.Xdebug简介 ...
- NBUT 1218 You are my brother
$dfs$. 记录一下每一个节点的深度就可以了. #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- Eden的退役记
好久没更博客了, 这篇随笔不同于之前的学术性随笔.游记,只是来发泄一下自己的情感,回忆一下自己的OI经历…… 五年的OI生涯结束了 初一:懵懂的我刚接触了OI,被其功能吸引.由于运气好过了初赛,然而复 ...
- python3-开发进阶 heapq模块(如何查找最大或最小的N个元素)
一.怎样从一个集合中获得最大或者最小的 N 个元素列表? heapq 模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题. import heapq nums = ...