(完善核心代码编程编程题2 失灵的键盘 100分限定语言:C(clang11),C(clang11),Java(javac 1.8), Python3(3.9),JavaScript Node(12.18.2),Go(1.14.4), Rust(1.44)知识点排序、字符串、输入的读取解析题目描述有一个键盘有2个按键失灵了按下这些键时会连续输出其他键对应的字符两次。具体如下-按下j键一次屏幕上显示uu两个连续的u按下b键一次屏幕上显示tt两个连续的t-u键和t键是好的按下u键一次时屏幕只会显示一次u正常按键按下t键一次时屏幕只会显示一次t正常按键)-假定屏幕上连续显示两个t一定是按了一次b键而不是两次t键假定按键t之后不会紧接着按键b即tttt转义为两个b而不可能是tbtu和j同样适用该规则-其它按键也都正常工作为了方便维修给定一串屏幕上输出的字符串维修师傅要求按照按键次数降序输出次数相同的按键按照对应字符的升序排序失灵按键以原对应字符来排序只统计按键次数大于0的按键同时维修师傅要求输出时需要进行一次转义字符映射规则如下-按键0~9直接以数字0~9进行输出按键a~z以10~35进行输出------------输入一个字符串s只包含小写字母和数字s的长度不超过500s中不包含b和字母)输出按键转义后的值按键次数)构成结果对所有按键的结果对按照按键次数降序排列输出实际是二维数组第二维固定长度2------------补充说明按键范围只包括小写字母a~z和数字0~9。转义表0-0, 1-1 ,..., 9-9a10 , b-11, ...., Z- 35以下是针对题目要求的多种编程语言实现。每个实现都遵循以下逻辑解析输入字符串还原按键序列遇到连续tt时记录为按一次b键遇到连续uu时记录为按一次j键单个t或u记录为对应按键其他字符直接记录为对应按键统计按键次数并转义数字字符转义为对应数字0-9字母字符转义为10-35a-10, b-11, ..., z-35过滤按键次数0的项排序按键次数降序次数相同时按键转义值升序输出二维数组[转义值, 按键次数]C 实现#include vector #include map #include algorithm #include cctype std::vectorstd::vectorint solve(std::string s) { std::mapchar, int countMap; int i 0, n s.size(); while (i n) { if (s[i] t) { if (i 1 n s[i1] t) { countMap[b]; i 2; } else { countMap[t]; i; } } else if (s[i] u) { if (i 1 n s[i1] u) { countMap[j]; i 2; } else { countMap[u]; i; } } else { countMap[s[i]]; i; } } std::vectorstd::vectorint result; for (const auto pair : countMap) { int escapeVal; if (std::isdigit(pair.first)) { escapeVal pair.first - 0; } else { escapeVal 10 (pair.first - a); } result.push_back({escapeVal, pair.second}); } std::sort(result.begin(), result.end(), [](const std::vectorint a, const std::vectorint b) { return a[1] b[1] ? a[0] b[0] : a[1] b[1]; }); return result; }C 实现#include stdio.h #include stdlib.h #include string.h #include ctype.h typedef struct { int escape; int count; } Entry; int compare(const void* a, const void* b) { const Entry* ea (const Entry*)a; const Entry* eb (const Entry*)b; return (ea-count eb-count) ? (ea-escape - eb-escape) : (eb-count - ea-count); } Entry* solve(const char* s, int* returnSize) { int count[36] {0}; int i 0, n strlen(s); while (i n) { if (s[i] t) { if (i 1 n s[i1] t) { count[11]; i 2; } else { count[20]; i; } } else if (s[i] u) { if (i 1 n s[i1] u) { count[19]; i 2; } else { count[21]; i; } } else { char c s[i]; int idx (c 0 c 9) ? (c - 0) : (10 (c - a)); count[idx]; i; } } *returnSize 0; for (int j 0; j 36; j) { if (count[j] 0) (*returnSize); } Entry* result (Entry*)malloc(*returnSize * sizeof(Entry)); int idx 0; for (int j 0; j 36; j) { if (count[j] 0) { result[idx].escape j; result[idx].count count[j]; idx; } } qsort(result, *returnSize, sizeof(Entry), compare); return result; }JavaScript 实现function solve(s) { const countMap new Map(); let i 0; while (i s.length) { if (s[i] t) { if (i 1 s.length s[i1] t) { countMap.set(b, (countMap.get(b) || 0) 1); i 2; } else { countMap.set(t, (countMap.get(t) || 0) 1); i; } } else if (s[i] u) { if (i 1 s.length s[i1] u) { countMap.set(j, (countMap.get(j) || 0) 1); i 2; } else { countMap.set(u, (countMap.get(u) || 0) 1); i; } } else { countMap.set(s[i], (countMap.get(s[i]) || 0) 1); i; } } const result []; for (const [key, count] of countMap.entries()) { const escapeVal key 0 key 9 ? key.charCodeAt(0) - 0.charCodeAt(0) : 10 (key.charCodeAt(0) - a.charCodeAt(0)); result.push([escapeVal, count]); } result.sort((a, b) b[1] - a[1] || a[0] - b[0]); return result; }Java 实现import java.util.*; public class Solution { public Listint[] solve(String s) { MapCharacter, Integer countMap new HashMap(); int i 0, n s.length(); while (i n) { char c s.charAt(i); if (c t) { if (i 1 n s.charAt(i1) t) { countMap.put(b, countMap.getOrDefault(b, 0) 1); i 2; } else { countMap.put(t, countMap.getOrDefault(t, 0) 1); i; } } else if (c u) { if (i 1 n s.charAt(i1) u) { countMap.put(j, countMap.getOrDefault(j, 0) 1); i 2; } else { countMap.put(u, countMap.getOrDefault(u, 0) 1); i; } } else { countMap.put(c, countMap.getOrDefault(c, 0) 1); i; } } Listint[] result new ArrayList(); for (Map.EntryCharacter, Integer entry : countMap.entrySet()) { char key entry.getKey(); int escapeVal (key 0 key 9) ? (key - 0) : (10 (key - a)); result.add(new int[]{escapeVal, entry.getValue()}); } result.sort((a, b) - a[1] b[1] ? Integer.compare(a[0], b[0]) : Integer.compare(b[1], a[1])); return result; } }Python 实现def solve(s): count_map {} i 0 n len(s) while i n: if s[i] t: if i 1 n and s[i1] t: count_map[b] count_map.get(b, 0) 1 i 2 else: count_map[t] count_map.get(t, 0) 1 i 1 elif s[i] u: if i 1 n and s[i1] u: count_map[j] count_map.get(j, 0) 1 i 2 else: count_map[u] count_map.get(u, 0) 1 i 1 else: count_map[s[i]] count_map.get(s[i], 0) 1 i 1 result [] for char, count in count_map.items(): escape_val ord(char) - ord(0) if 0 char 9 else 10 (ord(char) - ord(a)) result.append([escape_val, count]) result.sort(keylambda x: (-x[1], x[0])) return resultGo 实现package main import ( sort ) func solve(s string) [][]int { countMap : make(map[byte]int) i : 0 for i len(s) { switch s[i] { case t: if i1 len(s) s[i1] t { countMap[b] i 2 } else { countMap[t] i } case u: if i1 len(s) s[i1] u { countMap[j] i 2 } else { countMap[u] i } default: countMap[s[i]] i } } type entry struct{ escape, count int } var entries []entry for char, cnt : range countMap { var escapeVal int if 0 char char 9 { escapeVal int(char - 0) } else { escapeVal 10 int(char-a) } entries append(entries, entry{escapeVal, cnt}) } sort.Slice(entries, func(i, j int) bool { if entries[i].count ! entries[j].count { return entries[i].count entries[j].count } return entries[i].escape entries[j].escape }) result : make([][]int, len(entries)) for i, e : range entries { result[i] []int{e.escape, e.count} } return result }说明所有实现均遵循题目要求的解析规则和输出格式输入字符串s长度不超过 500仅包含小写字母不含 b 和 j和数字输出为二维数组元素为[转义值, 按键次数]按键次数降序排列次数相同按键转义值升序排列C 语言实现需调用free()释放返回的指针内存