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(好)的更多相关文章

  1. 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)

    [LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...

  2. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  3. [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 ...

  4. Leetcode: Reconstruct Original Digits from English

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  5. 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 ...

  6. 【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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

  10. 423. Reconstruct Original Digits from English

    这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...

随机推荐

  1. three.js、webGL、canvas区别于关联

    canvas是html5新定义的一个标签,用于做图形容器 webgl要依赖canvas运行. three.js是以webgl为基础的库,封装了一些3D渲染需求中重要的工具方法与渲染循环.

  2. 【python】if __name__ == '__main__'

    转载自:http://www.cnblogs.com/xuxm2007/archive/2010/08/04/1792463.html 当你打开一个.py文件时,经常会在代码的最下面看到if __na ...

  3. MFC宏常识

    1.宏就是用宏定义指令#define定义一个标识符,用它来表示一个字符串或一段源代码. MFC宏作为MFC类库的一个组成部分在MFC应用程序中经常出现. MFC宏在路径 ".../Micro ...

  4. Android学习之Android studio篇-Android Studio快捷键总结(mac)

    原文:http://blog.csdn.net/hudfang/article/details/52117065 符号代表键盘按键:⌘(command).⌥(option).⇧(shift).⇪(ca ...

  5. 用matplotlib绘制漫画风格的图表

    自从有了计算机,便很少有人手工绘制图表了.计算机绘制出的图表横平竖直,可以随意使用各种颜色,也完全不用担心画错需要重来的问题. 但有没有一种感觉,看多了工整的图表,变得有些审美疲劳.在各行各业逐渐过渡 ...

  6. 03 java 基础:注释 关键字 标识符 JShell

    Java 10 中已有 Jshell 工具,方便用户在其中直接输入相关 java 代码. 注释:java 中分为单行注释 //   多行注释 /*   */   文档注释 /**  */ 关键字:在 ...

  7. 初始pip

    关于pip包括下面的东西还不是很懂,慢慢的了解,我的pip是从https://bootstrap.pypa.io/get-pip.py 粘贴并命名为 get-pip.py 后,执行 python ge ...

  8. PAT L3-011. 直捣黄龙

    最短路. 先求出一个只包含最短路的$DAG$,然后在$DAG$上$dp$一下就可以了.英文变数字还有$map$转一下. #include<map> #include<set> ...

  9. 动若脱兔:深入浅出angr--初步理解符号执行以及angr架构

    一:概论 angr作为符号执行的工具,集成了过去的许多分析方式,它不仅能进行动态符号执行,而且还能进行很多静态分析,他在分析二进制程序中能发挥很大的作用,下面为一些应用: 1:利用符号执行探究执行路径 ...

  10. Codeforces Round #476 (Div. 2) [Thanks, Telegram!] ABCDE

    修仙场,没脑子,C边界判错一直在写mdzz..D根本没怎么想. 第二天起来想了想D这不是水题吗立马A了.看了看E一开始想分配问题后来发觉想岔了,只需要每次都把树最后分配的点移到最前面就好了. A. P ...