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

Note:

  1. Input contains only lowercase English letters.
  2. 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.
  3. Input length is less than 50,000.

Example 1:

Input: "owoztneoer"

Output: "012"

Example 2:

Input: "fviefuro"

Output: "45"

解法:原来的字符串是乱序的,当遇到一个字母 e 的时候,并没有办法知道这个字符是属于 one、three、nine等中的一个。但是当遇到一个 z 的时候,肯定
是可以确定这个字符是属于 zero 的,因为z只出现在zero中,同理,x 也只出现在six中。
那么这就是一个优先级的问题,应该优先匹配那些属于少数数字的字符,例如优先寻找z,如果出现一个z,那么必须相应的出现e、r、o。如果字符串里面已经没有z了,
那么这个时候如果还出现了r,那么这个 r 就只可能属于 three、four中的一个了,依次类推,就能得到解法。
    for (char c : s.toCharArray()) {
count[c]++;
}
int[] res = new int[10];
char[] idx = {'z', 'x', 's', 'v', 'f', 'r', 'w', 'g', 'o', 'i'};
//char[] idx = {'z', 'x', 's', 'v', 'f', 'r', 'w', 'g', 'o', 'i'};
for (char i : idx) {
while (count[i] > 0) {
switch (i) {
case 'z': //匹配z,减去zero
res[0]++;
count['z']--;
count['e']--;
count['r']--;
count['o']--;
break;
case 'x': //匹配six,减去six
res[6]++;
count['s']--;
count['i']--;
count['x']--;
break;
case 's': //匹配seven,因为之前优先匹配了six,如果这个时候,还有多余的s,那么肯定是seven
res[7]++;
count['s']--;
count['e']--;
count['v']--;
count['e']--;
count['n']--;
break;
case 'v': //匹配five,因为之前优先匹配了seven,如果这个时候还出现v,那么肯定是属于five
res[5]++;
count['f']--;
count['i']--;
count['v']--;
count['e']--;
break;
case 'f': //匹配four,因为之前匹配了four,那么这个时候还出现f,那么肯定属于four
res[4]++;
count['f']--;
count['o']--;
count['u']--;
count['r']--;
break;
case 'r': //匹配three,因为之前匹配了four,还出现three,肯定属于three
res[3]++;
count['t']--;
count['h']--;
count['r']--;
count['e']--;
count['e']--;
break;
case 'w':
res[2]++;
count['t']--;
count['w']--;
count['o']--;
break;
case 'g':
res[8]++;
count['e']--;
count['i']--;
count['g']--;
count['h']--;
count['t']--;
break;
case 'o':
res[1]++;
count['o']--;
count['n']--;
count['e']--;
break;
case 'i':
res[9]++;
count['n']--;
count['i']--;
count['n']--;
count['e']--;
break;
default:
break;
}
}
}
//System.out.print(res[0]);
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 9; i++) {
for (int j = 0; j < res[i]; j++) {
sb.append(i);
}
}
return sb.toString();
}

 

[LeetCode] 423 Reconstruct Original Digits from English的更多相关文章

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

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

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

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

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

  6. 423 Reconstruct Original Digits from English 从英文中重建数字

    给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字.注意:    输入只包含小写英文字母.    输入保证合法并可以转换为原始的数字,这意味着像 "ab ...

  7. 423. Reconstruct Original Digits from English

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

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

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

  9. Leetcode: Reconstruct Original Digits from English

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

随机推荐

  1. Java堆、栈和常量池以及相关String的详细讲解(经典中的经典) (转)

    原文链接 : http://www.cnblogs.com/xiohao/p/4296088.html 一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的 ...

  2. ROW_NUMBER over (order by **)

    ROW_NUMBER必须指写over (order by **),有时我根本就不想排序,想按原始顺序 )) AS ROWNUM,* FROM t

  3. Android 学习 (一)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...

  4. div显示提示信息

    div显示提示信息 <body> <style type="text/css"> a.link{position:relative;} a.link div ...

  5. 使用 Eclipse 插件部署 Java 应用

    打开 Eclipse,点击顶部的菜单『Help/Install New Software/Add』. 选择对话框顶部『Work with』 后面的『Add』按钮,并点击『Archive』选择下载到本地 ...

  6. Unity3D中以任意格式获取时间(C# .net也可用)

    最近楼主在开发中遇到了一个小问题 需要保存截图,同时把时间作为截图的名字存储 时间的保存格式为 2016-12-08 13:15:00 保存截图的流程就不说了,这篇博客只说一下以任意的格式保存时间. ...

  7. 使用merge同时执行insert和update操作

    SQL点滴18—SqlServer中的merge操作,相当地风骚   今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在tech ...

  8. NGUI 使用EventDelegate.Add与UIInput.onSubmit、UIInput.onChange限定编辑框中的内容

    Unity中,使用NGUI,通常为某个控件(如按钮)绑定事件(单击.双击.拖拽.滚轮.选择等)都是用UIEventListener,比如: public void Bind() { UIEventLi ...

  9. linux操作系统-脚本入门

    背景:在使用linux时,经常会写一些linux命令片段,比较麻烦,有经验的程序员会把 这些碎片式的命令写成shell脚本 1.重启tomcat脚本 #!/bin/sh #kill tomcat pi ...

  10. javascript eval和JSON之间的联系

    原出处:http://www.jb51.net/article/21688.htm eval函数的工作原理 eval函数会评估一个给定的含有JavaScript代码的字符串,并且试图去执行包含在字符串 ...