当前位置:网站首页>第六站神京门户-------手机号码的转换
第六站神京门户-------手机号码的转换
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
边栏推荐
- 使用zerotier让异地设备组局域网
- Notes on concurrent programming of vegetables (V) thread safety and lock solution
- Swagger2 自定义参数注解如何不显示
- MySql常用语句
- Derivation and regularization
- Idea - indexing or scanning files to index every time you start
- Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
- Resolution and size of mainstream mobile phones
- SQL Server 游标循环表数据
- 206. Reverse linked list (linked list)
猜你喜欢
Yarn core parameter configuration
The courses bought at a high price are open! PHPer data sharing
【leetcode】107. Sequence traversal of binary tree II
使用zerotier让异地设备组局域网
Comparison and practice of prototype design of knowledge service app
JVM——》常用命令
Deploy jar package
JVM——》常用参数
Detailed explanation of MapReduce calculation process
JVM - common parameters
随机推荐
高价买来的课程,公开了!phper资料分享
全栈交叉编译X86完成过程经验分享
206、反转链表(链表)
Latex usage
Notes on concurrent programming of vegetables (V) thread safety and lock solution
349、两个数组的交集
Deploy jar package
Let the LAN group use the remote device
59. Spiral matrix (array)
Example of pop-up task progress bar function based on pyqt5
203、移出链表元素(链表)
Strongest date regular expression
二叉树的构建和遍历
VScode
Xdotool key Wizard
The courses bought at a high price are open! PHPer data sharing
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
Comparison and practice of prototype design of knowledge service app
How can swagger2 custom parameter annotations not be displayed
19. Delete the penultimate node of the linked list (linked list)