How does C++ Leetcode reconstruct numbers from English?
What I want to share with you in this article is about how C++ Leetcode can reconstruct numbers from English. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Title
Analysis.
First of all, we analyze the composition of each letter, and then we find that some characters appear in only one word. Let's count the number of these words.
The number of characters in the s string is counted with a hash table, so we can know the number of characters in the s string. Then we pay attention to the characters that only appear in the two numbers.
H only appears in 3Jing 8. Since we already know the number of times 8 appears, we can calculate the number of times 3 appears.
F only appears in 4. 5. Since we already know the number of occurrences of 4, we can calculate the number of occurrences of 5.
S only appears in 6 and 7. Since we already know the number of 6 occurrences, we can calculate the number of 7 occurrences.
At this point, only 1 and 9 are not known, but we already know all the other numbers that contain o, so the remaining number is the number of 1.
Then there are only 9 with I at this time, so just count the number of 9.
After counting the number of times, just arrange them in ascending order.
Code
C++
My code
Class Solution {public: string originalDigits (string s) {unordered_map m; string nums [10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; string res; for (char ch: s) m [ch] + + / / 0 if (m ['z'] > 0) {for (int iTun0; I 0) {int x = m ['w']; for (int iTun0; I 0) {int x = m ['u']; for (int iTun0; I 0) {int x = m ['f'] For (int ionome0; I 0) {int x = m ['x']; for (int iTun0; I 0) {int x = m ['s']; for (int ionome0; I 0) {int x = m ['g']; for (int iTun0) I 0) {int x = m ['o']; for (int iTun0; I 0) {int x = m [t']; for (int iTun0; I 0) {int x = m ['i']; for (int iTun0; I)