1. 程式人生 > >423. Reconstruct Original Digits from English (leetcode)

423. Reconstruct Original Digits from English (leetcode)

one pty valid 個數 contain tput form end turn

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)