当前位置:网站首页>剑指 Offer 05. 替换空格
剑指 Offer 05. 替换空格
2022-07-05 08:16:00 【程序员·小李】
题目:
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = "We are happy."
输出:"We%20are%20happy."
限制:
0 <= s 的长度 <= 10000
思路:
1. 先进行一次遍历,查看空格的个数,空格需要替换成“%20”,因此需要对数组进行扩容。array.length - count + count * 3便是替换后数组的大小。
2. 从后向前依次复制。
class Solution {
public String replaceSpace(String s) {
if (s == null){
return null;
}
char[] array = s.toCharArray();
int count = 0;
for (char c : array){
if (c == ' '){
count++;
}
}
char[] newArray = new char[array.length - count + count * 3];
int pointer = newArray.length - 1;
for (int j = array.length - 1; j >= 0; j--){
if (array[j] != ' '){
newArray[pointer--] = array[j];
continue;
}
newArray[pointer--] = '0';
newArray[pointer--] = '2';
newArray[pointer--] = '%';
}
return new String(newArray);
}
}
边栏推荐
- Communication standard -- communication protocol
- MySQL MHA high availability cluster
- STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
- Halcon's practice based on shape template matching [2]
- Use indent to format code
- Ble encryption details
- Shell script
- Imx6ull bare metal development learning 1-assembly lit LED
- After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
- C WinForm [display real-time time in the status bar] - practical exercise 1
猜你喜欢
MHA High available Cluster for MySQL
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Step motor generates S-curve upper computer
NTC thermistor application - temperature measurement
Process communication mode between different hosts -- socket
MySQL之MHA高可用集群
Halcon's practice based on shape template matching [2]
STM32 single chip microcomputer - external interrupt
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
随机推荐
Naming rules for FreeRTOS
Vofa+ software usage record
C WinForm [exit application] - practice 3
Basic embedded concepts
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
Charge pump boost principle - this article will give you a simple understanding
Imx6ull bare metal development learning 2- use C language to light LED indicator
STM32 single chip microcomputer - external interrupt
Factors affecting the quality of slip rings in production
Step motor generates S-curve upper computer
99 multiplication table (C language)
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
C WinForm [display real-time time in the status bar] - practical exercise 1
C language # and #
My-basic application 2: my-basic installation and operation
On boost circuit
STM32 --- serial port communication
My-basic application 1: introduction to my-basic parser
The firmware of the connected j-link does not support the following memory access