当前位置:网站首页>[daily 3 questions (3)] reformat the phone number
[daily 3 questions (3)] reformat the phone number
2022-06-29 11:12:00 【Programmed ape without hair loss 2】

2 Brother :3 Sister , Stop watching soap operas , Today, let's do an algorithm problem .
3 Sister turned off the TV , Happy to run over .
3 Sister : Yes, yes ,java I have learned all the data structures of , Let's just put the horse here .
subject :
Give you a phone number in string form number .number By digital 、 Space ’ '、 And dash ‘-’ form .
Please reformat the phone number as follows .
First , Delete All the spaces and dashes .
secondly , Move the array from left to right Every time 3 In groups Block , until be left over 4 Number or less . The remaining numbers will be subdivided as follows :
2 A digital : Single containing 2 A block of numbers .
3 A digital : Single containing 3 A block of numbers .
4 A digital : The two contain 2 A block of numbers .
Finally, we use dashes to connect the blocks . Be careful , During reformatting Should not be Generation contains only 1 A block of numbers , also most Generate two with 2 A block of numbers .
Returns the formatted phone number .
Example 1:
Input :number = “1-23-45 6”
Output :“123-456”
explain : The number is “123456”
step 1: More than 4 A digital , So take it first 3 There are numbers in a group . The first 1 The block is “123” .
step 2: be left over 3 A digital , Put them in a single containing 3 A block of numbers . The first 2 The block is “456” .
Join these blocks and you get “123-456” .
Example 2:
Input :number = “123 4-567”
Output :“123-45-67”
explain : The number is “1234567”.
step 1: More than 4 A digital , So take it first 3 There are numbers in a group . The first 1 The block is “123” .
step 2: be left over 4 A digital , So divide them into two parts 2 A block of numbers . this 2 The blocks are “45” and “67” .
Join these blocks and you get “123-45-67” .
Example 3:
Input :number = “123 4-5678”
Output :“123-456-78”
explain : The number is “12345678” .
step 1: The first 1 Block “123” .
step 2: The first 2 Block “456” .
step 3: be left over 2 A digital , Put them in a single containing 2 A block of numbers . The first 3 The block is “78” .
Join these blocks and you get “123-456-78” .
Example 4:
Input :number = “12”
Output :“12”
Example 5:
Input :number = "–17-5 229 35-39475 "
Output :“175-229-353-94-75”
Tips :
2 <= number.length <= 100
number By numbers and characters ‘-’ And ’ ’ form .
number It contains at least 2 A digital .
java Code :
class Solution {
public String reformatNumber(String number) {
number = number.replace(" ", "");
number = number.replace("-", "");
int n = number.length();
StringBuilder sbd = new StringBuilder();
if(n <=3) {
sbd.append(number);
return sbd.toString();
}
int duan = n/3;
int yu = n%3;
for(int i=0;i<duan-1;i++) {
sbd.append(number.substring(3*i, 3*i+3)).append("-");
}
if(yu ==0) {
sbd.append(number.substring(n-3));
}else if(yu == 1) {
sbd.append(number.substring(n-4, n-2)).append("-").append(number.substring(n-2));
}else {
sbd.append(number.substring(n-5, n-2)).append("-").append(number.substring(n-2));
}
return sbd.toString();
}
}
边栏推荐
- 【每日3题(2)】生成交替二进制字符串的最少操作数
- 西门子S7-200SMART控制步进电机的具体方法及示例程序
- 内存分配——静态存储区 栈 堆 与static变量
- 当技术人成长为 CEO,应该修改哪些“Bug”?
- Here comes the tutorial of datawhale recommendation system!
- Bs-gx-018 student examination system based on SSM
- 9 easy to use JSON processing tools, greatly improving efficiency!
- How to properly manage temporary files when writing shell scripts
- By asp Net core downloading files according to the path
- 中国计算语言学大会、全国知识图谱与语义计算大会赛题火热进行中
猜你喜欢

Interview questions of Tencent automation software test of CSDN salary increase secret script (including answers)

任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过

ModbusTCP协议WIFI无线学习型单路红外模块(小壳版)

在线SQL转HTMLTable工具

Week 12 experiment -- implementation of VGA protocol based on FPGA

什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手

Multi thread communication between client and server (primary version)

what? It's amazing that you can read the whole comic book for free. You can't learn to be a money saver together

Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Version 2) 全球生态系统数据集

数据分析方法与思维:漏斗分析
随机推荐
Specific method and example program of Siemens s7-200smart control stepping motor
Reids设计与实现
Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)
When the "Ai x scientific computing" is in progress, Huawei's mindspore competition question is hot, waiting for you!
X-Frame-Options(点击劫持) 网页劫持漏洞
(JS)手写bind函数
ZABBIX monitors various MySQL indicators
Shell 引号和转义从来很少被人注意,但平时写脚本又经常用
Add notification announcements to send notifications to online users
嵌入式学习书籍推荐[通俗易懂]
Recommended embedded learning books [easy to understand]
(JS) observer mode
[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
STM32F1与STM32CubeIDE编程实例-超声波测距传感器驱动
涂鸦云开发 demo 登录
crypto 1~5
Nuc980 open source project 16- start from SPI flash (w25q128)
Thoroughly understand JVM Series 7: what is the flow process of objects in the generational model?
西门子S7-200SMART控制步进电机的具体方法及示例程序
(JS) responsibility chain mode