当前位置:网站首页>Sword finger offer 05 Replace spaces
Sword finger offer 05 Replace spaces
2022-07-05 08:21:00 【Programmer Xiao Li】
subject :
Please implement a function , Put the string s Replace each space in with "%20".
Example 1:
Input :s = "We are happy."
Output :"We%20are%20happy."
Limit :
0 <= s The length of <= 10000
Ideas :
1. Go through the first time , Check the number of spaces , Spaces need to be replaced with “%20”, Therefore, the array needs to be expanded .array.length - count + count * 3 Is the size of the array after replacement .
2. Copy from front to back .
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);
}
}
边栏推荐
- C WinForm [help interface - send email] - practice five
- Buildroot system for making raspberry pie cm3
- STM32 single chip microcomputer - bit band operation
- STM32 --- serial port communication
- Stm32--- systick timer
- Use indent to format code
- Communication standard -- communication protocol
- Summary -st2.0 Hall angle estimation
- How to copy formatted notepad++ text?
- Measurement fitting based on Halcon learning [i] fuse Hdev routine
猜你喜欢
Shell script
MHA High available Cluster for MySQL
如何写Cover Letter?
Negative pressure generation of buck-boost circuit
实例008:九九乘法表
C WinForm [change the position of the form after running] - Practical Exercise 4
Installation and use of libjpeg and ligpng
Briefly talk about the identification protocol of mobile port -bc1.2
STM32---IIC
UE像素流,来颗“减肥药”吧!
随机推荐
Semiconductor devices (I) PN junction
Briefly talk about the identification protocol of mobile port -bc1.2
On boost circuit
Circleq of linked list
關於線性穩壓器的五個設計細節
Relationship between line voltage and phase voltage, line current and phase current
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Network port usage
如何写Cover Letter?
关于线性稳压器的五个设计细节
Management and use of DokuWiki (supplementary)
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
Drive LED -- GPIO control
Vofa+ software usage record
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Bluetooth hc-05 pairing process and precautions
Stm32--- systick timer
Nb-iot technical summary
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
PMSM dead time compensation