Leetcode-423 Reconstruct Original Digits from English(从英文中重建数字)

 1 class Solution
 2 {
 3     public:
 4         string originalDigits(string s)
 5         {
 6             vector<int> CharacterList(26,0);
 7             for(auto c:s)
 8             {
 9                 CharacterList[c-‘a‘] ++;
10             }
11
12             string result;
13             if(CharacterList[‘g‘-‘a‘])
14             {
15                 result.append(CharacterList[‘g‘-‘a‘],‘8‘);
16                 CharacterList[‘e‘-‘a‘] -= CharacterList[‘g‘-‘a‘];
17                 CharacterList[‘i‘-‘a‘] -= CharacterList[‘g‘-‘a‘];
18                 CharacterList[‘h‘-‘a‘] -= CharacterList[‘g‘-‘a‘];
19                 CharacterList[‘t‘-‘a‘] -= CharacterList[‘g‘-‘a‘];
20                 CharacterList[‘g‘-‘a‘] = 0;
21             }
22             if(CharacterList[‘x‘-‘a‘])
23             {
24                 result.append(CharacterList[‘x‘-‘a‘],‘6‘);
25                 CharacterList[‘s‘-‘a‘] -= CharacterList[‘x‘-‘a‘];
26                 CharacterList[‘i‘-‘a‘] -= CharacterList[‘x‘-‘a‘];
27                 CharacterList[‘x‘-‘a‘] = 0;
28             }
29             if(CharacterList[‘s‘-‘a‘])
30             {
31                 result.append(CharacterList[‘s‘-‘a‘],‘7‘);
32                 CharacterList[‘e‘-‘a‘] -= 2*CharacterList[‘s‘-‘a‘];
33                 CharacterList[‘v‘-‘a‘] -= CharacterList[‘s‘-‘a‘];
34                 CharacterList[‘n‘-‘a‘] -= CharacterList[‘s‘-‘a‘];
35                 CharacterList[‘s‘-‘a‘] = 0;
36             }
37             if(CharacterList[‘v‘-‘a‘])
38             {
39                 result.append(CharacterList[‘v‘-‘a‘],‘5‘);
40                 CharacterList[‘f‘-‘a‘] -= CharacterList[‘v‘-‘a‘];
41                 CharacterList[‘i‘-‘a‘] -= CharacterList[‘v‘- ‘a‘];
42                 CharacterList[‘e‘-‘a‘] -= CharacterList[‘v‘-‘a‘];
43                 CharacterList[‘v‘-‘a‘] = 0;
44             }
45             if(CharacterList[‘i‘-‘a‘])
46             {
47                 result.append(CharacterList[‘i‘-‘a‘],‘9‘);
48                 CharacterList[‘n‘-‘a‘] -= 2*CharacterList[‘i‘-‘a‘];
49                 CharacterList[‘e‘-‘a‘] -= CharacterList[‘i‘-‘a‘];
50                 CharacterList[‘i‘-‘a‘] = 0;
51             }
52             if(CharacterList[‘u‘-‘a‘])
53             {
54                 result.append(CharacterList[‘u‘-‘a‘],‘4‘);
55                 CharacterList[‘f‘-‘a‘] -= CharacterList[‘u‘-‘a‘];
56                 CharacterList[‘o‘-‘a‘] -= CharacterList[‘u‘-‘a‘];
57                 CharacterList[‘r‘-‘a‘] -= CharacterList[‘u‘-‘a‘];
58                 CharacterList[‘u‘-‘a‘] = 0;
59             }
60             if(CharacterList[‘h‘-‘a‘])
61             {
62                 result.append(CharacterList[‘h‘-‘a‘],‘3‘);
63                 CharacterList[‘t‘-‘a‘] -= CharacterList[‘h‘-‘a‘];
64                 CharacterList[‘r‘-‘a‘] -= CharacterList[‘h‘-‘a‘];
65                 CharacterList[‘e‘-‘a‘] -= 2*CharacterList[‘h‘-‘a‘];
66                 CharacterList[‘h‘-‘a‘] = 0;
67             }
68             if(CharacterList[‘w‘-‘a‘])
69             {
70                 result.append(CharacterList[‘w‘-‘a‘],‘2‘);
71                 CharacterList[‘t‘-‘a‘] -= CharacterList[‘w‘-‘a‘];
72                 CharacterList[‘o‘-‘a‘] -= CharacterList[‘w‘-‘a‘];
73                 CharacterList[‘w‘-‘a‘] = 0;
74             }
75             if(CharacterList[‘z‘-‘a‘])
76             {
77                 result.append(CharacterList[‘z‘-‘a‘],‘0‘);
78                 CharacterList[‘e‘-‘a‘] -= CharacterList[‘z‘-‘a‘];
79                 CharacterList[‘r‘-‘a‘] -= CharacterList[‘z‘-‘a‘];
80                 CharacterList[‘o‘-‘a‘] -= CharacterList[‘z‘-‘a‘];
81                 CharacterList[‘z‘-‘a‘] = 0;
82             }
83             if(CharacterList[‘o‘-‘a‘])
84             {
85                 result.append(CharacterList[‘o‘-‘a‘],‘1‘);
86                 CharacterList[‘n‘-‘a‘] -= CharacterList[‘o‘-‘a‘];
87                 CharacterList[‘e‘-‘a‘] -= CharacterList[‘o‘-‘a‘];
88                 CharacterList[‘o‘-‘a‘] = 0;
89             }
90             sort(result.begin(),result.end());
91             return result;
92         }
93 };

原文地址:https://www.cnblogs.com/Asurudo/p/9486368.html

时间: 2024-10-15 21:46:15

Leetcode-423 Reconstruct Original Digits from English(从英文中重建数字)的相关文章

[LeetCode] Reconstruct Original Digits from English 从英文中重建数字

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 origina

LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路

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 origina

[LeetCode]423. Reconstruct Original Digits from English

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 origina

423. Reconstruct Original Digits from English (leetcode)

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 origina

Leetcode: Reconstruct Original Digits from English

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 origina

[Swift]LeetCode423. 从英文中重建数字 | Reconstruct Original Digits from English

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 origina

[LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点

Starting with an?undirected?graph (the "original graph") with nodes from?0?to?N-1, subdivisions are made to some of the edges. The graph is given as follows:?edges[k]?is a list of integer pairs?(i, j, n)?such that?(i, j)?is an edge of the origin

LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重复数字,并且返回不重复数字的个数 遍历操作: 可以使用新的for循环 for (int n : nums){} 每次进行对比,并且更新第一个遇到不相等的元素的下标为i 对数组进行重新赋值操作 当数组长度大于1时,ans初值为1,当数组长度为0时,返回0 参考代码 : package leetcode_5

[LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字

Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. Note: 1 ≤ k ≤ n ≤ 109. Example: Input: n: 13 k: 2 Output: 10 Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so