马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
标题形貌
九宫格按键输入,输出表现内容。有英文和数字两个模式,默认是数字模式。数字模式直接输出数字,英文模式一连按同一个按键会依次出现这个按键上的字母。假如输入“/”或其他字符,则循环停止。
输入形貌
输入范围为数字0~9和字符#、/。输出屏幕表现。比方在数字模式下,输入1234,表现1234;在英文模式下,输入1234,表现,adg。
输出形貌
- #用于切换模式,默认是数字模式,实验#后切换为英文模式。
- /表现延伸,比方在英文模式下,输入22/222,表现为bc。
- 英文模式下,多次按同一键,比方输入22222,表现为b。
解题思绪
这个标题可以通过状态机来办理。我们必要维护当前模式(数字模式或英文模式)以及当前按键的一连按击次数。根据输入字符的差别,我们更新状态并天生相应的输出。
代码实现
Java
- import java.util.*;
- public class NineKeyInput {
- private static final String[] DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
- private static final String[] LETTERS = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
- public String processInput(String input) {
- boolean isNumberMode = true;
- StringBuilder output = new StringBuilder();
- int index = 0;
- while (index < input.length()) {
- char ch = input.charAt(index);
- if (ch == '#') {
- isNumberMode = !isNumberMode;
- index++;
- } else if (ch == '/') {
- index++;
- } else if (Character.isDigit(ch)) {
- int digit = ch - '0';
- if (isNumberMode) {
- output.append(DIGITS[digit]);
- index++;
- } else {
- int count = 0;
- while (index < input.length() && input.charAt(index) == ch) {
- count++;
- index++;
- }
- String letters = LETTERS[digit];
- output.append(letters.charAt((count - 1) % letters.length()));
- }
- } else {
- index++;
- }
- }
- return output.toString();
- }
- public static void main(String[] args) {
- NineKeyInput processor = new NineKeyInput();
- System.out.println(processor.processInput("123#222235/56")); // 123adjjm
- }
- }
复制代码 Python
- class NineKeyInput:
- DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
- LETTERS = [" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
- def process_input(self, input_str):
- is_number_mode = True
- output = []
- index = 0
- while index < len(input_str):
- ch = input_str[index]
- if ch == '#':
- is_number_mode = not is_number_mode
- index += 1
- elif ch == '/':
- index += 1
- elif ch.isdigit():
- digit = int(ch)
- if is_number_mode:
- output.append(self.DIGITS[digit])
- index += 1
- else:
- count = 0
- while index < len(input_str) and input_str[index] == ch:
- count += 1
- index += 1
- letters = self.LETTERS[digit]
- output.append(letters[(count - 1) % len(letters)])
- else:
- index += 1
- return ''.join(output)
- processor = NineKeyInput()
- print(processor.process_input("123#222235/56")) # 123adjjm
复制代码 C++
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class NineKeyInput {
- private:
- vector<string> DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
- vector<string> LETTERS = {" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
- public:
- string processInput(string input) {
- bool isNumberMode = true;
- string output;
- int index = 0;
- while (index < input.length()) {
- char ch = input[index];
- if (ch == '#') {
- isNumberMode = !isNumberMode;
- index++;
- } else if (ch == '/') {
- index++;
- } else if (isdigit(ch)) {
- int digit = ch - '0';
- if (isNumberMode) {
- output += DIGITS[digit];
- index++;
- } else {
- int count = 0;
- while (index < input.length() && input[index] == ch) {
- count++;
- index++;
- }
- string letters = LETTERS[digit];
- output += letters[(count - 1) % letters.length()];
- }
- } else {
- index++;
- }
- }
- return output;
- }
- };
- int main() {
- NineKeyInput processor;
- cout << processor.processInput("123#222235/56") << endl; // 123adjjm
- return 0;
- }
复制代码 JavaScript
- class NineKeyInput {
- constructor() {
- this.DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
- this.LETTERS = [" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
- }
- processInput(input) {
- let isNumberMode = true;
- let output = '';
- let index = 0;
- while (index < input.length) {
- const ch = input[index];
- if (ch === '#') {
- isNumberMode = !isNumberMode;
- index++;
- } else if (ch === '/') {
- index++;
- } else if (/\d/.test(ch)) {
- const digit = parseInt(ch);
- if (isNumberMode) {
- output += this.DIGITS[digit];
- index++;
- } else {
- let count = 0;
- while (index < input.length && input[index] === ch) {
- count++;
- index++;
- }
- const letters = this.LETTERS[digit];
- output += letters[(count - 1) % letters.length];
- }
- } else {
- index++;
- }
- }
- return output;
- }
- }
- const processor = new NineKeyInput();
- console.log(processor.processInput("123#222235/56")); // 123adjjm
复制代码 复杂度分析
- 时间复杂度: O(n),此中n是输入字符串的长度。我们必要遍历整个字符串一次来处置惩罚每个字符。
- 空间复杂度: O(1),除了输出字符串外,我们只利用了常数空间来存储模式和当前按键的一连按击次数。
总结
我们利用了状态机来处置惩罚这个标题,通过维护当前模式和按键的一连按击次数,可以或许有用地天生相应的输出。这种方法简单直观,实用于处置惩罚这种模式切换和按键映射的标题。
|