题解 | #简单密码#哈希表快速解决
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); Map<Character, Character> map = new HashMap<>(); map.put('a', '2'); map.put('b', '2'); map.put('c', '2'); map.put('d', '3'); map.put('e', '3'); map.put('f', '3'); map.put('g', '4'); map.put('h', '4'); map.put('i', '4'); map.put('j', '5'); map.put('k', '5'); map.put('l', '5'); map.put('m', '6'); map.put('n', '6'); map.put('o', '6'); map.put('p', '7'); map.put('q', '7'); map.put('r', '7'); map.put('s', '7'); map.put('t', '8'); map.put('u', '8'); map.put('v', '8'); map.put('w', '9'); map.put('x', '9'); map.put('y', '9'); map.put('z', '9'); char[] arr = new char[str.length()]; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch >= 'a' && ch <= 'z') { arr[i] = map.get(ch); } else if (ch >= 'A' && ch <= 'Z') { ch += 32; if (ch == 'z') { arr[i] = 'a'; } else { ch++; arr[i] = ch; } } else { arr[i] = ch; } } String res = new String(arr); System.out.print(res); } }