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"足够笨的办法
class Solution {public: string originalDigits(string s) { int table[26] = {0}; int len = s.length(), pos0 = 0, pos2 = 0; string m[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; for(char ch : s) table[ch - 'a']++; string ans; for(int i = 0; i < table['z' - 'a']; ++i){ table['e' - 'a']--; table['r' - 'a']--; table['o' - 'a']--; ans += "0"; } pos0 = table['z' - 'a']; for(int i = 0; i < table['w' - 'a']; ++i){ table['t' - 'a']--; table['o' - 'a']--; ans += "2"; } pos2 = pos0 + table['w' - 'a']; for(int i = 0; i < table['u' - 'a']; ++i){ table['f' - 'a']--; table['o' - 'a']--; table['r' - 'a']--; ans += "4"; } int num = table['r' - 'a']; ans.insert(pos2, num, '3'); table['t' - 'a'] -= num; table['h' - 'a'] -= num; table['e' - 'a'] -= 2 * num; for(int i = 0; i < table['f' - 'a']; ++i){ table['v' - 'a']--; table['i' - 'a']--; table['e' - 'a']--; ans += "5"; } for(int i = 0; i < table['x' - 'a']; ++i){ table['s' - 'a']--; table['i' - 'a']--; ans += "6"; } for(int i = 0; i < table['s' - 'a']; ++i){ table['e' - 'a'] -= 2; table['v' - 'a']--; table['n' - 'a']--; ans += "7"; } for(int i = 0; i < table['g' - 'a']; ++i){ table['e' - 'a']--; table['i' - 'a']--; table['h' - 'a']--; table['t' - 'a']--; ans += "8"; } for(int i = 0; i < table['i' - 'a']; ++i){ table['n' - 'a'] -= 2; table['e' - 'a']--; ans += "9"; } num = table['e' - 'a']; ans.insert(pos0, num, '1'); return ans; }};新闻热点
疑难解答