华为OD机试 - 九宫格按键输入 - 逻辑分析(Java 2023 B卷 200分)

[复制链接]
发表于 2025-10-13 13:49:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
标题形貌

九宫格按键输入,输出表现内容。有英文和数字两个模式,默认是数字模式。数字模式直接输出数字,英文模式一连按同一个按键会依次出现这个按键上的字母。假如输入“/”或其他字符,则循环停止。
输入形貌

输入范围为数字0~9和字符#、/。输出屏幕表现。比方在数字模式下,输入1234,表现1234;在英文模式下,输入1234,表现,adg。
输出形貌


  • #用于切换模式,默认是数字模式,实验#后切换为英文模式。
  • /表现延伸,比方在英文模式下,输入22/222,表现为bc。
  • 英文模式下,多次按同一键,比方输入22222,表现为b。
解题思绪

这个标题可以通过状态机来办理。我们必要维护当前模式(数字模式或英文模式)以及当前按键的一连按击次数。根据输入字符的差别,我们更新状态并天生相应的输出。
代码实现

Java
  1. import java.util.*;
  2. public class NineKeyInput {
  3.     private static final String[] DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
  4.     private static final String[] LETTERS = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
  5.     public String processInput(String input) {
  6.         boolean isNumberMode = true;
  7.         StringBuilder output = new StringBuilder();
  8.         int index = 0;
  9.         while (index < input.length()) {
  10.             char ch = input.charAt(index);
  11.             if (ch == '#') {
  12.                 isNumberMode = !isNumberMode;
  13.                 index++;
  14.             } else if (ch == '/') {
  15.                 index++;
  16.             } else if (Character.isDigit(ch)) {
  17.                 int digit = ch - '0';
  18.                 if (isNumberMode) {
  19.                     output.append(DIGITS[digit]);
  20.                     index++;
  21.                 } else {
  22.                     int count = 0;
  23.                     while (index < input.length() && input.charAt(index) == ch) {
  24.                         count++;
  25.                         index++;
  26.                     }
  27.                     String letters = LETTERS[digit];
  28.                     output.append(letters.charAt((count - 1) % letters.length()));
  29.                 }
  30.             } else {
  31.                 index++;
  32.             }
  33.         }
  34.         return output.toString();
  35.     }
  36.     public static void main(String[] args) {
  37.         NineKeyInput processor = new NineKeyInput();
  38.         System.out.println(processor.processInput("123#222235/56")); // 123adjjm
  39.     }
  40. }
复制代码
Python
  1. class NineKeyInput:
  2.     DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  3.     LETTERS = [" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
  4.     def process_input(self, input_str):
  5.         is_number_mode = True
  6.         output = []
  7.         index = 0
  8.         while index < len(input_str):
  9.             ch = input_str[index]
  10.             if ch == '#':
  11.                 is_number_mode = not is_number_mode
  12.                 index += 1
  13.             elif ch == '/':
  14.                 index += 1
  15.             elif ch.isdigit():
  16.                 digit = int(ch)
  17.                 if is_number_mode:
  18.                     output.append(self.DIGITS[digit])
  19.                     index += 1
  20.                 else:
  21.                     count = 0
  22.                     while index < len(input_str) and input_str[index] == ch:
  23.                         count += 1
  24.                         index += 1
  25.                     letters = self.LETTERS[digit]
  26.                     output.append(letters[(count - 1) % len(letters)])
  27.             else:
  28.                 index += 1
  29.         return ''.join(output)
  30. processor = NineKeyInput()
  31. print(processor.process_input("123#222235/56"))  # 123adjjm
复制代码
C++
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. class NineKeyInput {
  6. private:
  7.     vector<string> DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
  8.     vector<string> LETTERS = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
  9. public:
  10.     string processInput(string input) {
  11.         bool isNumberMode = true;
  12.         string output;
  13.         int index = 0;
  14.         while (index < input.length()) {
  15.             char ch = input[index];
  16.             if (ch == '#') {
  17.                 isNumberMode = !isNumberMode;
  18.                 index++;
  19.             } else if (ch == '/') {
  20.                 index++;
  21.             } else if (isdigit(ch)) {
  22.                 int digit = ch - '0';
  23.                 if (isNumberMode) {
  24.                     output += DIGITS[digit];
  25.                     index++;
  26.                 } else {
  27.                     int count = 0;
  28.                     while (index < input.length() && input[index] == ch) {
  29.                         count++;
  30.                         index++;
  31.                     }
  32.                     string letters = LETTERS[digit];
  33.                     output += letters[(count - 1) % letters.length()];
  34.                 }
  35.             } else {
  36.                 index++;
  37.             }
  38.         }
  39.         return output;
  40.     }
  41. };
  42. int main() {
  43.     NineKeyInput processor;
  44.     cout << processor.processInput("123#222235/56") << endl; // 123adjjm
  45.     return 0;
  46. }
复制代码
JavaScript
  1. class NineKeyInput {
  2.     constructor() {
  3.         this.DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  4.         this.LETTERS = [" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
  5.     }
  6.     processInput(input) {
  7.         let isNumberMode = true;
  8.         let output = '';
  9.         let index = 0;
  10.         while (index < input.length) {
  11.             const ch = input[index];
  12.             if (ch === '#') {
  13.                 isNumberMode = !isNumberMode;
  14.                 index++;
  15.             } else if (ch === '/') {
  16.                 index++;
  17.             } else if (/\d/.test(ch)) {
  18.                 const digit = parseInt(ch);
  19.                 if (isNumberMode) {
  20.                     output += this.DIGITS[digit];
  21.                     index++;
  22.                 } else {
  23.                     let count = 0;
  24.                     while (index < input.length && input[index] === ch) {
  25.                         count++;
  26.                         index++;
  27.                     }
  28.                     const letters = this.LETTERS[digit];
  29.                     output += letters[(count - 1) % letters.length];
  30.                 }
  31.             } else {
  32.                 index++;
  33.             }
  34.         }
  35.         return output;
  36.     }
  37. }
  38. const processor = new NineKeyInput();
  39. console.log(processor.processInput("123#222235/56")); // 123adjjm
复制代码
复杂度分析


  • 时间复杂度: O(n),此中n是输入字符串的长度。我们必要遍历整个字符串一次来处置惩罚每个字符。
  • 空间复杂度: O(1),除了输出字符串外,我们只利用了常数空间来存储模式和当前按键的一连按击次数。
总结

我们利用了状态机来处置惩罚这个标题,通过维护当前模式和按键的一连按击次数,可以或许有用地天生相应的输出。这种方法简单直观,实用于处置惩罚这种模式切换和按键映射的标题。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表