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"

思路一:

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

代码如下:

import java.util.HashMap;
public class Solution {
static HashMap<String, Integer> hashMap;
public static void replace(String str)
{
hashMap.replace(str, hashMap.get(str)-1);
}
public String originalDigits(String s) {
int [] ans=new int[10];
hashMap= new HashMap<String, Integer>();
String str=null;
for(int i=0;i<26;i++)
{
str=String.valueOf((char)(i+97));
hashMap.put(str,0);
}
for (int i = 0; i < s.length(); i++) {
str=s.substring(i, i+1);
hashMap.replace(str, hashMap.get(str)+1);
}
while(hashMap.get("z")>0)
{
replace("z");
replace("e");
replace("r");
replace("o");
ans[0]+=1;
}
while(hashMap.get("w")>0)
{
replace("t");
replace("w");
replace("o");
ans[2]+=1;
}
while(hashMap.get("u")>0)
{
replace("f");
replace("o");
replace("u");
replace("r");
ans[4]+=1;
}
while(hashMap.get("x")>0)
{
replace("s");
replace("i");
replace("x");
ans[6]+=1;
}
while(hashMap.get("g")>0)
{
replace("e");
replace("i");
replace("g");
replace("h");
replace("t");
ans[8]+=1;
}
while(hashMap.get("o")>0)
{
replace("o");
replace("n");
replace("e");
ans[1]+=1; }
while(hashMap.get("t")>0)
{
replace("t");
replace("h");
replace("r");
replace("e");
replace("e");
ans[3]+=1;
}
while(hashMap.get("f")>0)
{
replace("f");
replace("i");
replace("v");
replace("e");
ans[5]+=1;
}
while(hashMap.get("s")>0)
{
replace("s");
replace("e");
replace("v");
replace("e");
replace("n");
ans[7]+=1;
}
while(hashMap.get("i")>0)
{
replace("n");
replace("i");
replace("n");
replace("e");
ans[9]+=1;
} StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 9; i++){
for (int j = 0; j < count[i]; j++){
sb.append(i);
}
}
return sb.toString(); }
}

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

代码如下:

public String originalDigits(String s) {
int[] count = new int[10];
for (int i = 0; i < s.length(); i++){ if (c == 'z') count[0]++;
if (c == 'w') count[2]++;
if (c == 'x') count[6]++;
if (c == 'g') count[8]++;
if (c == 'u') count[4]++;
  if (c == 's') count[7]++;
if (c == 'f') count[5]++;
if (c == 'h') count[3]++;
if (c == 'i') count[9]++;
if (c == 'o') count[1]++;
}
count[7] -= count[6];//(six,seven都有s,那么s的总数量减去6的数量就是7的数量),下面同理
count[5] -= count[4];
count[3] -= count[8];
count[9] = count[9] - count[8] - count[5] - count[6];
count[1] = count[1] - count[0] - count[2] - count[4];
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 9; i++){
for (int j = 0; j < count[i]; j++){
sb.append(i);
}
}
return sb.toString();
}
 

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. PostgreSQL使用MyBatis,insert时返回主键

    MyBatis中普通的insert语句是这样的: <insert id="insert" parameterType="com.xxx.xxx.xxDo" ...

  2. Linux Ubuntu从零开始部署web环境及项目 -----部署项目 (三)

    上一篇讲了如何在linux搭建web环境,这边将如何部署项目. 1,打包项目包 2,上传项目包 将.war项目包通过xftp上传到tomcat目录wabapps目录下 3,启动项目 通过xshell命 ...

  3. PHPFastCGI进程管理器PHP-FPM详解

    PHP-FPM是一个PHPFastCGI进程管理器,是只用于PHP的.      PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将它patch到你的PH ...

  4. 小符号反映大问题,Shell中下划线_与变量的关系。

    之前写过一个根据日期和时间自动命名文件名的时候遇到一个问题. #! /bin/bash read -p "please input the filename:" filename ...

  5. 【前端】深入浅出Javascript中的数值转换

    由于Javascript是一门弱类型的语言,在我们的代码中无时无刻不在发生着类型转换,所以了解Javascript中的类型转换对于了解我们认识Javascript的运行原理至关重要. 本文主要从数值转 ...

  6. 在Pycharm中使用jupyter笔记本

    在Pycharm中使用jupyter笔记本 我在Pycharm中使用jupyter笔记本,发现新的Jupyter更新中,增加了令牌. 随着创建的虚拟环境启动的所有设置,并将URL设置为127.0.0. ...

  7. (Java后端 Java web)面试时如何展示自己非技术方面的能力(其实就是综合能力)

    这篇文章的适用范围其实不仅限于Java后端或Java Web,不过其中有些是拿这方面举例的,在其它方面,大家可以举一反三,应该也能得到些启示. 我们在面试时,会发现有些候选人技术不错,比如在Java ...

  8. css3弹性盒模型flex快速入门与上手1

    一.什么是flex? flex是css3中引入的一种布局方式,可以非常灵活高效控制元素的排列与对齐方式,大多人称之为弹性布局. 二.怎么使用flex? 任何一个容器都可以指定为flex布局 #box ...

  9. springMVC中的redirect和forward区别?

    1.forward在跳转后可以取到message值,redirect在跳转后无法取到message值. 2.forward跳转后地址栏URL不会改变,而redirect会改变.

  10. C++格式化硬盘程序

    #include using namespace std; //声明命名空间 void main() {  char format[12]="format", name[10], ...