当前位置:网站首页>第六站神京门户-------手机号码的转换
第六站神京门户-------手机号码的转换
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
边栏推荐
- The courses bought at a high price are open! PHPer data sharing
- Swagger2 接口如何导入Postman
- Solution architect's small bag - 5 types of architecture diagrams
- Hikvision face to face summary
- SQL Server 游标循环表数据
- 精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
- 最强日期正则表达式
- 全栈交叉编译X86完成过程经验分享
- MapReduce core and foundation demo
- 域名和IP地址的联系
猜你喜欢

Let the LAN group use the remote device

/etc/shadow可以破解吗?

Windows installs redis and sets the redis service to start automatically

SQL Server 递归查询上下级

How can swagger2 custom parameter annotations not be displayed

【leetcode】102.二叉树的层序遍历

Jinglianwen technology - professional data annotation company and intelligent data annotation platform

中职网络安全2022国赛之CVE-2019-0708漏洞利用

Learning Notes 6 - Summary of several deep learning convolutional neural networks

How to quickly download vscode
随机推荐
部署jar包
142. Circular linked list||
MySQL common statements
A diary of dishes | 238 Product of arrays other than itself
Is the pointer symbol of C language close to variable type or variable name?
/Can etc / shadow be cracked?
454、四数之和(哈希表)
Image processing - Noise notes
C语言——自定义类型
【leetcode】107. Sequence traversal of binary tree II
/etc/shadow可以破解吗?
域名和IP地址的联系
Deploy jar package
1、两数之和(哈希表)
206. Reverse linked list (linked list)
MapReduce core and foundation demo
What are the system events of Jerry's [chapter]
Derivation and regularization
Hikvision face to face summary
What are Jerry's usual program exceptions? [chapter]


