Leetcode: Reconstruct Original Digits from English
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"
# of '0': # of 'z'
# of '2': # of 'w'
4: u
6: x
8: g
3: h - 8
5: f - 4
7: s - 6
1: o - 0 - 2 - 4
9: i - 5 - 6 - 8
public String originalDigits(String s) {
int[] count = new int[10];
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if (c == 'z') count[0]++;
if (c == 'w') count[2]++;
if (c == 'x') count[6]++;
if (c == 's') count[7]++; //7-6
if (c == 'g') count[8]++;
if (c == 'u') count[4]++;
if (c == 'f') count[5]++; //5-4
if (c == 'h') count[3]++; //3-8
if (c == 'i') count[9]++; //9-8-5-6
if (c == 'o') count[1]++; //1-0-2-4
}
count[7] -= count[6];
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();
}
我的code用了一个数组来存char count
public class Solution {
public String originalDigits(String s) {
StringBuilder res = new StringBuilder();
if (s==null || s.length()==0) return "";
int[] chars = new int[26];
int[] digits = new int[10];
for (int i=0; i<s.length(); i++) {
chars[s.charAt(i)-'a']++;
}
count(chars, digits);
for (int i=0; i<digits.length; i++) {
for (int j=0; j<digits[i]; j++) {
res.append(i);
}
}
return res.toString();
}
public void count(int[] chars, int[] digits) {
//'0'
digits[0] = chars['z'-'a'];
//'2'
digits[2] = chars['w'-'a'];
//'4'
digits[4] = chars['u'-'a'];
//'6'
digits[6] = chars['x'-'a'];
//'8'
digits[8] = chars['g'-'a'];
//'1' and '2' and '0' and '4' share 'o'
digits[1] = chars['o'-'a'] - digits[2] - digits[0] - digits[4];
//'3' and '8' share 'h'
digits[3] = chars['h'-'a'] - digits[8];
//'5' and '4' share 'f'
digits[5] = chars['f'-'a'] - digits[4];
//'7' and '6' share 's'
digits[7] = chars['s'-'a'] - digits[6];
//'9' and '5' and '6' and '8' share 'i'
digits[9] = chars['i'-'a'] - digits[5] - digits[6] - digits[8];
}
}
Leetcode: Reconstruct Original Digits from English的更多相关文章
- [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】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 ...
- [Swift]LeetCode423. 从英文中重建数字 | 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 ...
随机推荐
- 【POJ】3243 Clever Y
http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...
- UICollectionView集合视图的概念
如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...
- 20145330第七周《Java学习笔记》
20145330第七周<Java学习笔记> 第十三章 时间与日期 认识时间与日期 时间的度量 GMT(格林威治标准时间):现在不是标准时间 世界时(UT):1972年UTC出来之前,UT等 ...
- zabbix3.2.0beta2 监控模版
Zabbix监控中用到了一系列模版,nginx后端检测状态 微信告警等一系列常规的服务应用监控 memcached监控模版,可以自己重新定义memcached的端口 http://files.cnbl ...
- 接口(Java)
什么是接口:接口就是一些方法特征的集合,接口是对抽象的抽象. 在java语言中,接口有两种意思: ①概念性的接口,即系统对外提供的所有服务 ②指用interface关键字定义的接口,也称为接口类型 特 ...
- spring security方法一 自定义数据库表结构
Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也无法满足所有企业内部对用户信息和权限信息管理的要求.基本上每个企业内部都有一套自己的用户信息管理结构,同时也 ...
- Codeforces Round #196 (Div. 2)
A 题意:O(-1) 思路:排个序搞定. B 题意:O(-1) 思路:坑了我好久,这个框框水平垂直比例固定,分两种情况即可,不能旋转,我想多了,分了四种情况. C 题意:一列n个位置,让你填m个数,当 ...
- javascript模拟jQuery封装委托事件,兼容IE
var $ = function(id){ var dom = document.getElementById(id); return { on:function(eventType,element, ...
- php连接多数据库
<?php $conn1=mysql_connect('localhost','root','','new_link '); $conn2=mysql_connect('localhost',' ...
- ngrok访问外网
1. 外网映射工具介绍 windows用户: 1,下载windows版本的客户端,解压到你喜欢的目录2,在命令行下进入到path/to/windows_386/下3,执行 ngrok -config= ...