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 digits in ascending order.
Note:
- Input contains only lowercase English letters.
- 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. - 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)的更多相关文章
- 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- [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 ...
- 【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(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 从英文中重建数字
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字.注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 "ab ...
- 423. Reconstruct Original Digits from English
这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...
- [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: Reconstruct Original Digits from English
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
随机推荐
- 201521123108 《Java程序设计》第11周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集多线程 Q1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法实现互斥同步访问 ...
- Eclipse rap 富客户端开发总结(15) :rap如何使用js
1. 把输入的字符串当 javascript 执行 try { RWT.getResponse().getWriter().println("alert('123');"); } ...
- 基于图形检测API(shape detection API)的人脸检测
原文:https://paul.kinlan.me/face-detection/ 在 Google 开发者峰会中,谷歌成员 Miguel Casas-Sanchez 跟我说:"嘿 Paul ...
- 《Head First Java》读书笔记(2) - Java面向对象思想
1.了解继承 对象继承实际上就是一种"is - a"的关系,如上图的"PantherMan is a SuperHero?",是,那么便属于继承的理解. 继承能 ...
- (一)关于java泛型的学习总结(泛型方法、泛型擦除)
目录概要 一.泛型方法 二.利用泛型方法的特性实现代码的简化 三. 关于泛型的擦除 四.无界通配符和原生类型区别 五.转型和警告 泛型 一般的类中的属性或方法的参数,只能使用具体的类型:要么是基本 ...
- Linux下利用expect,不用交互模式,直接登陆远程主机
Linux环境下只有在机器20.200.254.18上ssh dataconv@20.200.31.23才能连接到23的机器,而且还需要输入密码(每次都需要输入地址,密码很烦),所以利用expect写 ...
- JAVA多线程---高并发程序设计
先行发生原则 程序顺序原则:一个线程内保证语义的串行性 volatile:volatile变量的写,先发生于读,这保证了volatile变量的可见性 锁规则:解锁必然发生在随后的加锁前 传递性:A优先 ...
- 第6章 Overlapped I/O, 在你身后变戏法 ---被激发的 File Handles -3
最简单的 overlapped I/O 类型,是使用它自己的文件 handle 作为同步机制.首先你以 FILE_FLAG_OVERLAPPED 告诉 Win32 说你不要使用默认的同步 I/O.然后 ...
- 【POJ】 1061 青蛙的约会(扩欧)
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 119148 Accepted: 25070 Descript ...
- hdu4632
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...