题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
String val = "abcdefghijklmnopqrstuvwxyz";
Scanner in = new Scanner(System.in);
String str1 = in.nextLine();
String str2 = in.nextLine();
Set<Character> set=new HashSet<Character>();
StringBuilder stringBuilder = new StringBuilder();
//利用set去重复
for (int i = 0; i < str1.length(); i++) {
if (set.contains(str1.charAt(i))){
continue;
}else {
set.add(str1.charAt(i));
stringBuilder.append(str1.charAt(i));
}
}
//补全剩余字母
for (int i = 0; i < val.length(); i++) {
if (set.contains(val.charAt(i))) {
continue;
} else {
stringBuilder.append(val.charAt(i));
}
}
//加密
for (int i = 0; i < str2.length(); i++) {
int x=val.indexOf(str2.charAt(i));
System.out.print(stringBuilder.charAt(x));
}
}
}
华为OD机试 文章被收录于专栏
自己在准备机试,记录一下学习轨迹,主要参考真题,代码大部分是自己想的,不保证ac,仅供参考
查看22道真题和解析
小米集团公司氛围 394人发布