当前位置:网站首页>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);
}
}
边栏推荐
- Verilog -- state machine coding method
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- Explication de la procédure stockée pour SQL Server
- 如何写Cover Letter?
- 实例006:斐波那契数列
- List of linked lists
- Charge pump boost principle - this article will give you a simple understanding
- QEMU STM32 vscode debugging environment configuration
- Bluetooth hc-05 pairing process and precautions
- Ble encryption details
猜你喜欢
Array integration initialization (C language)
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
OC and OD gate circuit
Measurement fitting based on Halcon learning [i] fuse Hdev routine
Negative pressure generation of buck-boost circuit
How to copy formatted notepad++ text?
实例006:斐波那契数列
实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
随机推荐
Introduction of air gap, etc
Verilog -- state machine coding method
Simple design description of MIC circuit of ECM mobile phone
Negative pressure generation of buck-boost circuit
Tailq of linked list
【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
QEMU demo makefile analysis
Slist of linked list
C WinForm [change the position of the form after running] - Practical Exercise 4
QEMU STM32 vscode debugging environment configuration
Keil use details -- magic wand
Working principle and type selection of common mode inductor
Design a clock frequency division circuit that can be switched arbitrarily
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Classic application of MOS transistor circuit design (2) - switch circuit design
DokuWiki deployment notes
Solutions to compilation warnings in Quartus II
VESC Benjamin test motor parameters
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
99 multiplication table (C language)