当前位置:网站首页>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);
}
}
边栏推荐
- Imx6ull bare metal development learning 1-assembly lit LED
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- Zero length array in GNU C
- Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
- 动力电池UL2580测试项目包括哪些
- OC and OD gate circuit
- Talk about the circuit use of TVs tube
- Problem solving: interpreter error: no file or directory
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- STM32 tutorial triple ADC interleaved sampling
猜你喜欢

Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
![[paper reading] the latest transfer ability in deep learning: a survey in 2022](/img/6b/b564fb7a6895329073fb5eaff64340.png)
[paper reading] the latest transfer ability in deep learning: a survey in 2022
![C WinForm [get file path -- traverse folder pictures] - practical exercise 6](/img/8b/1e470de4e4ecd4fd1bb8e5cf23f466.jpg)
C WinForm [get file path -- traverse folder pictures] - practical exercise 6

MySQL之MHA高可用集群

如何写Cover Letter?

Semiconductor devices (I) PN junction

Briefly talk about the identification protocol of mobile port -bc1.2

Explain task scheduling based on Cortex-M3 in detail (Part 1)
![[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code](/img/58/576b6b77509ed7a9bef138f3899e37.jpg)
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code

Array integration initialization (C language)
随机推荐
实例010:给人看的时间
Shell script
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
OC and OD gate circuit
C language # and #
Carrier period, electrical speed, carrier period variation
Talk about the circuit use of TVs tube
STM32 --- configuration of external interrupt
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
QEMU STM32 vscode debugging environment configuration
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
Talk about the function of magnetic beads in circuits
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
關於線性穩壓器的五個設計細節
Some thoughts on extracting perspectives from ealfa and Ebeta
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
Stm32--- systick timer
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
Slist of linked list