leetcode 423. Reconstruct Original Digits from English

来源:互联网 发布:重庆大数据行动计划 编辑:程序博客网 时间:2024/05/16 05:51

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"


在经历了这么多逻辑思维题之后,这一题需要一种迷之思维。

package leetcode;import java.util.HashMap;public class Reconstruct_Original_Digits_from_English_423 {HashMap<Character, Integer> map = new HashMap<Character, Integer>();int[] resultInt = new int[10];public String originalDigits(String s) {char[] cs = s.toCharArray();for (int i = 0; i < cs.length; i++) {int count = map.getOrDefault(cs[i], 0);count++;map.put(cs[i], count);}// zero one two three four five six seven eight nine// z是zero的唯一标识,w是two的唯一标识,// u是four的唯一标识,x是six的唯一标识// g是eight的唯一标识// 不可以说n、i是nine的唯一标志(因为n、i有可能是one和six出现的ni)remove(0, 'z', new char[] { 'z', 'e', 'r', 'o' });remove(2, 'w', new char[] { 't', 'w', 'o' });remove(4, 'u', new char[] { 'f', 'o', 'u', 'r' });remove(6, 'x', new char[] { 's', 'i', 'x' });remove(8, 'g', new char[] { 'e', 'i', 'g', 'h', 't' });// 把上述都remove掉之后,剩下了:// one three five seven nine// 其中o时候one的唯一标识,t是three的唯一标识// f是five的唯一标识,s是seven的唯一标识remove(1, 'o', new char[] { 'o', 'n', 'e' });remove(3, 't', new char[] { 't', 'h', 'r', 'e', 'e' });remove(5, 'f', new char[] { 'f', 'i', 'v', 'e' });remove(7, 's', new char[] { 's', 'e', 'v', 'e', 'n' });// 最后只剩下nine了int nine_count = map.getOrDefault('i', 0);resultInt[9] = nine_count;String result = "";for (int i = 0; i <= 9; i++) {while (resultInt[i] > 0) {result += i;resultInt[i]--;}}return result;}public void remove(int digit, char unusualChar, char[] chars) {while (map.get(unusualChar) != null) {resultInt[digit]++;for (int i = 0; i < chars.length; i++) {int count = map.get(chars[i]);count--;if (count == 0) {map.remove(chars[i]);} else {map.put(chars[i], count);}}}}public static void main(String[] args) {// TODO Auto-generated method stubReconstruct_Original_Digits_from_English_423 r = new Reconstruct_Original_Digits_from_English_423();System.out.println(r.originalDigits("fviefuro"));}}
哈哈,大神也发现了这个窍门,然后也差不多是这个思路。
public String originalDigits(String s) {    if(s==null || s.length()==0) return "";    int[] count = new int[128];    for(int i=0;i<s.length();i++)  count[s.charAt(i)]++;    int[] num = new int[10];    num[0] = count['z']; //zero    num[2] = count['w']; //two    num[4] = count['u']; //four    num[6] = count['x']; //fix    num[8] = count['g']; //eight    num[1] = count['o']-count['z']-count['w']-count['u']; //one=[o]-zero-two-four    num[3] = count['h']-count['g']; //three=[h]-eight    num[5] = count['v']-count['s']+count['x']; //five=[v]-seven    num[7] = count['s']-count['x']; //seven=[s]-six    // nine=[i]-six-eight-five    num[9] = count['i']-count['x']-count['g']-count['v']+count['s']-count['x'];    String ret = new String();    for(int i=0;i<10;i++){    for(int j=num[i];j>0;j--){    ret += String.valueOf(i);    }    }                return ret;}


阅读全文
0 0
原创粉丝点击