423. Reconstruct Original Digits from English
这个题做得突出一个蠢字。。
思路就是看unique letter,因为题里说肯定是valid string。。
一开始有几个Z就有几个ZERO
同样的还有x for six, g for eight, w for two..
比如有x个six,那剩下的i的字母数量要-x,s-=x...
算完0682之后,剩下的数字里所有S都是属于seven的。。
减完之后,剩下所有V都是属于FIVE的。。
顺序有很多种,我是0682754931.
public class Solution
{
public String originalDigits(String str)
{
String res = "";
int[] l = new int[26];
for(int j = 0; j < str.length();j++) l[str.charAt(j) -'a']++;
int z = l['z'-'a'];
int e = l['e'-'a'];
int r = l['r'-'a'];
int o = l['o'-'a'];
int n = l['n'-'a'];
int t = l['t'-'a'];
int w = l['w'-'a'];
int h = l['h'-'a'];
int f = l['f'-'a'];
int u = l['u'-'a'];
int i = l['i'-'a'];
int v = l['v'-'a'];
int s = l['s'-'a'];
int x = l['x'-'a'];
int g = l['g'-'a'];
int[] nums = new int[10];
// zero
nums[0] = z;
e-=z; r-=z; o-=z;
// six
nums[6] = x;
s-=x; i-=x;
// eight
nums[8] = g;
e-=g; i-=g;h-=g;t-=g; g-=g;
// two
nums[2] = w;
t-=w;o-=w;
// seven
nums[7] = s;
s-=nums[7];e-=nums[7]*2;v-=nums[7];n-=nums[7];
// five
nums[5] = v;
f-=nums[5];i-=nums[5];v-=nums[5];e-=nums[5];
// four
nums[4] = f;
f-=nums[4];o-=nums[4];u-=nums[4];r-=nums[4];
// nine
nums[9] = i;
n-=nums[9]*2; i-=nums[9]; e-=nums[9];
// three
nums[3] = t;
t-=nums[3]; h-=nums[3]; r-=nums[3]; e-=nums[3]*2;
// one
nums[1] = o;
for(int m = 0; m < 10;m++)
{
for(int k = 0; k < nums[m];k++) res+=m;
}
return res;
}
}
423. Reconstruct Original Digits from English的更多相关文章
- 【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 (leetcode)
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 ...
- [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 ...
随机推荐
- java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
http://blog.csdn.net/agileclipse/article/details/17161225 详情请点击链接查看
- 第三篇、调优之路 Apache调优
1. 简介 在第一篇中整合了apache + tomcat ,利用了apache解析静态文件为tomcat解压.但是在测试机上发现两者性能不足,不能充分利用服务器的性能,该篇中将对apache进行性 ...
- 算法系列之图--BFS
广度优先搜索以源结点s为出发点,算法始终将已发现和未发现结点之间的边界,沿其广度方向向外扩展.也即算法需要在发现所有距离源结点s为k的所有结点之后才会去发现距离源结点距离为k+1的其他结点. talk ...
- HTML5-黑客帝国2D
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
- 使用php实现爬虫程序 套取网站的图片实例
<?php //去采集a67 图片 网站链接 http://www.xiamov.com/list/1/p.2 你也可以采集其他网站的图片 //创建链接 dedecms--a67 //设置执行不 ...
- 入门1:PHP的优点
一.语法简单 二.学习成本低 (因为语法简单 所以学习成本低) 三.开发效率高 (PHP运行流程很简单,语法也很简单,必然开发效率就高) 四.跨平台 (我们只需要写一份PHP的程序,就可以非常方便的把 ...
- Yii2的相关学习记录,前后台分离及migrate使用(七)
最近一直忙其它的(其实是懒!),将<深入理解Yii2>看了一遍,一些当初没明白的稍微明了了点,然后又看yii2的图片上传等处理.富文本.restful什么的,但由于没进行到这里,只看也不管 ...
- XAMPP配置虚拟主机
当你在本地进行单个网站建设和测试的时候,你只需要正常的安装一下XAMPP就好了.XAMPP本身是集成了apache.mysql和php的.然而当你本地测试站点一多的话,你就不得不考虑使用多个虚拟主机来 ...
- apt-cache madison package-name
apt-cache madison package-name 搜索软件有那些可用版本,
- WPF学习笔记-TextBox光标位置如何放到最后?
TextBox光标位置如何放到最后? 使用SelectionStart : TextBox.SelectionStart = TextBox.Text.Length;