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:

  1. Input: "owoztneoer"
  2.  
  3. Output: "012"

Example 2:

  1. Input: "fviefuro"
  2.  
  3. Output: "45"
  4.  

思路一:

  //1. 循环一遍,统计所有字母各自总数量
        //2.对 zero two  four six eight 统计(因为10个数字中,它们有各自独特的标记,分别是 z w u x g),出现标记一次,统计总数对相应的字母减1,如出现z,则zero4个字母都减去1
        //3.对剩下的数中,继续找独特点, 分别有 one three five seven (标记为 o t f s),统计总数对相应的字母减1
        //4.对剩下的nine 进行统计,i或者e出现几次,就有几个nine
   //   z one w three u five x seven g nine    for [ a b c.... ]   比如出现z就  zero 都-1

代码如下:

  1. import java.util.HashMap;
  2. public class Solution {
  3. static HashMap<String, Integer> hashMap;
  4. public static void replace(String str)
  5. {
  6. hashMap.replace(str, hashMap.get(str)-1);
  7. }
  8. public String originalDigits(String s) {
  9. int [] ans=new int[10];
  10. hashMap= new HashMap<String, Integer>();
  11. String str=null;
  12. for(int i=0;i<26;i++)
  13. {
  14. str=String.valueOf((char)(i+97));
  15. hashMap.put(str,0);
  16. }
  17. for (int i = 0; i < s.length(); i++) {
  18. str=s.substring(i, i+1);
  19. hashMap.replace(str, hashMap.get(str)+1);
  20. }
  21. while(hashMap.get("z")>0)
  22. {
  23. replace("z");
  24. replace("e");
  25. replace("r");
  26. replace("o");
  27. ans[0]+=1;
  28. }
  29. while(hashMap.get("w")>0)
  30. {
  31. replace("t");
  32. replace("w");
  33. replace("o");
  34. ans[2]+=1;
  35. }
  36. while(hashMap.get("u")>0)
  37. {
  38. replace("f");
  39. replace("o");
  40. replace("u");
  41. replace("r");
  42. ans[4]+=1;
  43. }
  44. while(hashMap.get("x")>0)
  45. {
  46. replace("s");
  47. replace("i");
  48. replace("x");
  49. ans[6]+=1;
  50. }
  51. while(hashMap.get("g")>0)
  52. {
  53. replace("e");
  54. replace("i");
  55. replace("g");
  56. replace("h");
  57. replace("t");
  58. ans[8]+=1;
  59. }
  60. while(hashMap.get("o")>0)
  61. {
  62. replace("o");
  63. replace("n");
  64. replace("e");
  65. ans[1]+=1;
  66.  
  67. }
  68. while(hashMap.get("t")>0)
  69. {
  70. replace("t");
  71. replace("h");
  72. replace("r");
  73. replace("e");
  74. replace("e");
  75. ans[3]+=1;
  76. }
  77. while(hashMap.get("f")>0)
  78. {
  79. replace("f");
  80. replace("i");
  81. replace("v");
  82. replace("e");
  83. ans[5]+=1;
  84. }
  85. while(hashMap.get("s")>0)
  86. {
  87. replace("s");
  88. replace("e");
  89. replace("v");
  90. replace("e");
  91. replace("n");
  92. ans[7]+=1;
  93. }
  94. while(hashMap.get("i")>0)
  95. {
  96. replace("n");
  97. replace("i");
  98. replace("n");
  99. replace("e");
  100. ans[9]+=1;
  101. }
  102.  
  103. StringBuilder sb = new StringBuilder();
  104. for (int i = 0; i <= 9; i++){
  105. for (int j = 0; j < count[i]; j++){
  106. sb.append(i);
  107. }
  108. }
  109. return sb.toString();
  110.  
  111. }
  112. }

但是以上代码比较冗长,把思路一转换一下,先对所有标记字符计数,再用总数减去相应的数量,得到一个正确的答案,就可以很简短的写出来,代码很容易理解

代码如下:

  1. public String originalDigits(String s) {
  2. int[] count = new int[10];
  3. for (int i = 0; i < s.length(); i++){
  4.  
  5. if (c == 'z') count[0]++;
  6. if (c == 'w') count[2]++;
  7. if (c == 'x') count[6]++;
  8. if (c == 'g') count[8]++;
  9. if (c == 'u') count[4]++;
  10.   if (c == 's') count[7]++;
  11. if (c == 'f') count[5]++;
  12. if (c == 'h') count[3]++;
  13. if (c == 'i') count[9]++;
  14. if (c == 'o') count[1]++;
  15. }
  16. count[7] -= count[6];//(six,seven都有s,那么s的总数量减去6的数量就是7的数量),下面同理
  17. count[5] -= count[4];
  18. count[3] -= count[8];
  19. count[9] = count[9] - count[8] - count[5] - count[6];
  20. count[1] = count[1] - count[0] - count[2] - count[4];
  21. StringBuilder sb = new StringBuilder();
  22. for (int i = 0; i <= 9; i++){
  23. for (int j = 0; j < count[i]; j++){
  24. sb.append(i);
  25. }
  26. }
  27. return sb.toString();
  28. }
  1.  

423. Reconstruct Original Digits from English (leetcode)的更多相关文章

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

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

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

  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】423. Reconstruct Original Digits from English

    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. 201521123045java课程设计---定时器

    #课程设计--定时器(201521123045 郑子熙) 1.团队课程设计博客链接 http://www.cnblogs.com/chendajia/p/7065730.html 2.个人负责模块或任 ...

  2. jQuery 简介,与js的对比

    jquery可以说是js的封装,大多数情况下jquery比js简单,它们两个可以相互写对方的里面,使用jquery需要导入jquery文件. <script src="jquery-1 ...

  3. Windows下用Composer安装Laravel步骤(集成php环境用phpStudy2016版本)

    描述:到官方网站了解并根据自己所需要的版本号安装,所需要的php版本是多少. 中文网站:http://www.golaravel.com/ 官方网站:https://laravel.com/ 其实各自 ...

  4. Intellij IDEA WEB结构目录说明【转载】

    https://my.oschina.net/lujianing/blog/186737?p=1#OSC_h2_1

  5. SQL基础巩固

    1.一定要记住,SQL 对大小写不敏感! 2.分号是在数据库系统中分隔每条 SQL 语句的标准方法,这样就可以在对服务器的相同请求中执行一条以上的语句. 如果您使用的是 MS Access 和 SQL ...

  6. ActiveMQ_Windows版本的安装部署

    1, 保证电脑上安装了jdk6以上版本的java,并配置了好环境变量 : 2, 官方下载地址:http://activemq.apache.org/download-archives.html ,这里 ...

  7. Openlayers系列(一)关于地图投影的理解

    背景 近期开发以MongoDB为基础的分布式地理数据管理平台系统,被要求做一个简单的demo给客户进行演示.于是笔者便打算向数据库中存储一部分瓦片数据,写一个简单的存取服务器,使用Openlayers ...

  8. String类的常见面试题(3)

    1.判断定义为String类型的s1和s2是否相等 String s1 = "abc"; //这个"abc"对象首先会进常量池 String s2 = &quo ...

  9. 工欲善其事,必先利其器之open live writer写作

    在博客园学习有一段时间,想想是不是自己也应该开始写点东西,做点总结,更加快速的提升自己. 查看小组/博客园使用帮助 得知目前windows live writer 已经停止更新并推荐安装 open l ...

  10. 在 macOS High Sierra 10.13 搭建 PHP 开发环境

    2017 年 9 月 26 日,苹果公司正式发布了新一代 macOS,版本为 High Sierra (11.13). macOS High Sierra 预装了 Ruby(2.3.3).PHP(7. ...