题解 | #简单密码#
简单密码
http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
站在巨人肩膀上的答案,善用map和set
参考答案:mans0n
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print(decode(sc.nextLine()));
}
public static String decode(String str){
Map<String, String> map = new HashMap<>();
map.put("1","1");
map.put("abc","2");
map.put("def","3");
map.put("ghi","4");
map.put("jkl","5");
map.put("mno","6");
map.put("pqrs","7");
map.put("tuv","8");
map.put("wxyz","9");
map.put("0","0");
char[] charArr = str.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : charArr){
if(c >= '0' && c <= '9')
sb.append(c);
else if(c >= 'A' && c <= 'Y'){
sb.append((char)(c+33));
}
else if(c == 'Z')
sb.append('a');
else{
Set<String> keys = map.keySet();
for(String k : keys){
if(k.contains(String.valueOf(c)))
sb.append(map.get(k));
}
}
}
return sb.toString();
}
}