当前位置:网站首页>第六站神京门户-------手机号码的转换
第六站神京门户-------手机号码的转换
2022-04-23 10:46:00 【威少总冠军】
这个题比较简单,水文一篇
Map与Set的使用
一、题目描述
题目链接
二、解题
- 将 key — value (字母 — 数字)放到map中
- 新建StringBuilder,遍历 初始的字符串,遇到 字母,通过map寻找到对应的数字,添加数字;遇到数字,直接添加 ;遇到 “-”,跳过即可;
- 当 StringBuilder 长度为 3 时,添加 “-”
public static void main431(String[] args) {
Scanner scanner = new Scanner(System.in);
// 赋值map的方式
Map<Character, Character> map = new HashMap<>();
String s1 = "ABCDEFGHIJKLMNOPQRSTYVWXYZ";
String s2 = "22233344455566677778889999";
char[] chars1 = s1.toCharArray();
char[] chars2 = s2.toCharArray();
for (int i = 0; i < chars1.length; i++) {
map.put(chars1[i], chars2[i]);
}
// 赋值 map 的方式
// char ch = 'A';
// Map<Character, Integer> map = new HashMap<>();
//
// for (int i = 2; i <= 9; i++) {
// if( i == 7 || i == 9){
// for (int j = 0; j < 4; j++) {
// map.put(ch++, i);
// }
// }else {
// for (int j = 0; j < 3; j++) {
// map.put(ch++, i);
// }
// }
// }
while(scanner.hasNext()){
int nums = scanner.nextInt();
String[] phones = new String[nums];
for (int i = 0; i < nums; i++) {
phones[i] = scanner.nextLine();
}
// TreeSet 在 HashSet 的基础上 增加排序(字典序)功能
TreeSet<String> set = transForm(phones, map);
for (String s : set) {
System.out.println(s);
}
System.out.println();
}
}
public static TreeSet<String> transForm(String[] phones, Map<Character, Character> map){
TreeSet<String> set = new TreeSet<>();
for (String phone : phones) {
StringBuilder stringBuffer = new StringBuilder();
for (int j = 0; j < phone.length(); j++) {
if (stringBuffer.length() == 3) {
stringBuffer.append('-');
}
char ch = phone.charAt(j);
if (ch == '-') {
continue;
}
if (ch >= '0' && ch <= '9') {
stringBuffer.append(ch);
} else {
stringBuffer.append(map.get(ch));
}
}
set.add(stringBuffer.toString());
}
return set;
}
鲁北田园,厚德载物
山东德州

版权声明
本文为[威少总冠军]所创,转载请带上原文链接,感谢
https://blog.csdn.net/gjwloveforever/article/details/124332533
边栏推荐
- 最强日期正则表达式
- Windows installs redis and sets the redis service to start automatically
- 全栈交叉编译X86完成过程经验分享
- Ueditor -- limitation of 4m size of image upload component
- Notes on concurrent programming of vegetables (V) thread safety and lock solution
- Define linked list (linked list)
- Strongest date regular expression
- 997. Square of ordered array (array)
- Read integrity monitoring techniques for vision navigation systems - 4 multiple faults in vision system
- Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
猜你喜欢

Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
Detailed explanation of MapReduce calculation process

Solution architect's small bag - 5 types of architecture diagrams

JVM——》常用参数

Reading integrity monitoring techniques for vision navigation systems - 3 background

Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP

得到知识服务app原型设计比较与实践

JVM——》常用命令
MapReduce core and foundation demo

SSH利用私钥无密钥连接服务器踩坑实录
随机推荐
Swagger2 接口如何导入Postman
349、两个数组的交集
Solution architect's small bag - 5 types of architecture diagrams
Esp32 learning - add folder to project
【leetcode】107. Sequence traversal of binary tree II
Yarn resource scheduler
C#和数据库连接中类的问题
域名和IP地址的联系
Let the LAN group use the remote device
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
UDP basic learning
Embedded related surface (I)
Learning notes 7-depth neural network optimization
Jinglianwen technology - professional data annotation company and intelligent data annotation platform
/Can etc / shadow be cracked?
Linked list intersection (linked list)
Is the pointer symbol of C language close to variable type or variable name?
App. In wechat applet JS files, components, APIs
997、有序数组的平方(数组)
MySql常用语句


