1
+ /* *
2
+ * 思路:从最末尾开始,有 ‘#’ 就取前两位的数字,没‘#’ 就取前一位的数字。按 map 转换成字符串
3
+ * AC: Runtime: 4 ms, faster than 39.48% of C++ online submissions for Decrypt String from Alphabet to Integer Mapping.
4
+ * Memory Usage: 7.4 MB, less than 100.00% of C++ online submissions for Decrypt String from Alphabet to Integer Mapping.
5
+ *
6
+ * T:O(n) S:O(1)
7
+ */
8
+ class Solution {
9
+ public:
10
+ string freqAlphabets (string s) {
11
+ int len = s.length (), pos = len - 1 ;
12
+ string ans;
13
+ unordered_map<string, string> int_to_str = {
14
+ {" 1" , " a" }, {" 2" , " b" }, {" 3" , " c" },{" 4" , " d" },{" 5" , " e" },{" 6" , " f" },
15
+ {" 7" , " g" },{" 8" , " h" },{" 9" ," i" },{" 10" ," j" },{" 11" , " k" },{" 12" , " l" },
16
+ {" 13" , " m" },{" 14" ," n" },{" 15" , " o" },{" 16" , " p" },{" 17" , " q" },{" 18" , " r" },
17
+ {" 19" , " s" },{" 20" , " t" },{" 21" , " u" },{" 22" , " v" },{" 23" , " w" }, {" 24" , " x" },
18
+ {" 25" , " y" },{" 26" , " z" }
19
+ };
20
+ while (pos >= 0 ) {
21
+ if (s[pos] == ' #' ) {
22
+ pos -= 2 ;
23
+ ans = int_to_str[s.substr (pos, 2 )] + ans;
24
+ } else {
25
+ ans = int_to_str[s.substr (pos, 1 )] + ans;
26
+ }
27
+ pos--;
28
+ }
29
+
30
+ return ans;
31
+ }
32
+ };
0 commit comments