423. Reconstruct Original Digits from English
来源:互联网 发布:五星体育网络源 编辑:程序博客网 时间:2024/06/05 06:14
Approach #1 Simple Solution with Hash Table[Accept]
Algorithm
First we can find the characters ‘z’ of “zero” is unique and ‘w’ of “two” is unique as well the ‘u’ of “four” and ‘x’ of “six”.
So we can easily count the number of ‘0’ , ‘2’ , ‘4’ ,’6’ .
According to this thought we will solve the problem with follow steps .
1 Calculate the number of characters
int hash[26]; memset( hash , 0 ,sizeof( hash ) ) ; for( int i=0 ; i < s.size() ; i ++) { hash[ s[i] - 'a' ] ++; }
2 New a array to save the count of number
int nums[10]; memset( nums , 0 , sizeof( nums ) );
3 Now we count the number of ‘0’, ‘2’ , ‘4’ , ‘6’ . But the point is when we count the number
,we should minus the other characters .
For example ,when we calculate the character ‘z’ of “zero”, meanwhile ,
we should minus the number of other character ‘e’ , ‘r’ , ‘o’ .
if( hash['z'-'a'] != 0) { nums[0] = hash['z'-'a']; hash['e'-'a'] -= nums[0]; hash['r'-'a'] -= nums[0]; hash['o'-'a'] -= nums[0];}
4 After we count the number of ‘0’ , ‘2’ , ‘4’ , ‘6’ . the ‘g’ of “eight” is unique .
5 After we count the number of ‘8’ . the ‘h’ of “three” is unique.
6 Similar of above .
c++
class Solution {public: string originalDigits(string s) { int hash[26] ; memset( hash , 0 , sizeof( hash ) ) ; for( int i = 0 ; i < s.size() ; i ++ ) { hash[ s[i] - 'a' ] ++ ; } int nums[10]; memset( nums , 0 , sizeof( nums ) ) ; //0 if( hash[ 'z' - 'a' ] ) { nums[0] = hash[ 'z' - 'a' ] ; hash[ 'e' - 'a' ] -= nums[0] ; hash[ 'r' - 'a' ] -= nums[0] ; hash[ 'o' - 'a' ] -= nums[0] ; } //2 if( hash[ 'w' - 'a' ] ) { nums[2] = hash[ 'w' - 'a' ] ; hash[ 't' - 'a' ] -= nums[2] ; hash[ 'o' - 'a' ] -= nums[2] ; } //4 if( hash[ 'u' - 'a' ] ) { nums[4] = hash[ 'u' - 'a' ] ; hash[ 'f' - 'a' ] -= nums[4] ; hash[ 'o' - 'a' ] -= nums[4] ; hash[ 'r' - 'a' ] -= nums[4] ; } //6 if( hash[ 'x' - 'a' ] ) { nums[6] = hash[ 'x' - 'a' ] ; hash[ 's' - 'a' ] -= nums[6] ; hash[ 'i' - 'a' ] -= nums[6] ; } //8 if( hash[ 'g' - 'a' ] ) { nums[8]=hash[ 'g' - 'a' ] ; hash[ 'e' - 'a' ] -= nums[8] ; hash[ 'i' - 'a' ] -= nums[8] ; hash[ 'h' - 'a' ] -= nums[8] ; hash[ 't' - 'a' ] -= nums[8] ; } //3 if( hash[ 'h' - 'a' ] ) { nums[3]=hash[ 'h' - 'a' ] ; hash[ 't' - 'a' ] -= nums[3] ; hash[ 'r' - 'a' ] -= nums[3] ; hash[ 'e' - 'a' ] -= nums[3] ; } //5 if( hash[ 'f' - 'a' ] ) { nums[5] = hash[ 'f' - 'a' ] ; hash[ 'i' - 'a' ] -= nums[5] ; hash[ 'v' - 'a' ] -= nums[5] ; hash[ 'e' - 'a' ] -= nums[5] ; } //1 if( hash[ 'o' - 'a' ] ) { nums[1] = hash[ 'o' - 'a' ] ; } //7 if( hash[ 's' - 'a' ] ) { nums[7] = hash[ 's' - 'a' ] ; } //9 if( hash[ 'i' - 'a' ] ) { nums[9] = hash[ 'i' - 'a' ] ; } string ans; for( int i = 0 ; i < 10 ; i ++ ) { while( nums[i] > 0 ) { ans.push_back( i + '0' ) ; nums[i] -- ; } } return ans; }};
Complexity Analysis
Time complexity :
O(n) . We takeO(n) to Calculate the number of characters ,then takeO(1) to carry out the operationSpace complexity :
O(1) .
- 423. Reconstruct Original Digits from English
- Leetcode 423. Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English
- 【leetcode】423. Reconstruct Original Digits from English
- [leetcode] 423. Reconstruct Original Digits from English
- LeetCode 423. Reconstruct Original Digits from English
- LeetCode 423. Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English
- 【LeetCode】 423. Reconstruct Original Digits from English
- [leetcode]423. Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English**
- LeetCode 423. Reconstruct Original Digits from English
- LeetCode 423. Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English
- Leetcode-423. Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English
- 423. Reconstruct Original Digits from English
- 2017 年你应该尝试的 25 个 Android 库
- Android解决不同分辨率距离一致的问题
- TextView使用drawable属性
- Fountains CodeForces
- 抽象类与接口的区别
- 423. Reconstruct Original Digits from English
- 一个简单的SpringMVC框架web程序
- 4. Median of Two Sorted Arrays
- “System.StackOverflowException”类型的未经处理的异常在 App_Code.qcryqgiv.dll 中发生
- javascript中的define用法
- 447. Number of Boomerangs
- yolo解
- jenkins使用Gerrit,错误checkout分支版本的解决方法
- Paragon NTFS for Mac 15 v15.0中文版