当前位置:网站首页>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);
}
}
边栏推荐
- Relationship between line voltage and phase voltage, line current and phase current
- C WinForm [exit application] - practice 3
- Count the number of inputs (C language)
- Ble encryption details
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- WiFi wpa_ Detailed description of supplicant hostpad interface
- Summary -st2.0 Hall angle estimation
- MySQL MHA high availability cluster
- Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
- H264 (I) i/p/b frame gop/idr/ and other parameters
猜你喜欢
Measurement fitting based on Halcon learning [i] fuse Hdev routine
STM32 --- serial port communication
Simple design description of MIC circuit of ECM mobile phone
Hardware and software solution of FPGA key chattering elimination
Array integration initialization (C language)
C # joint configuration with Halcon
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Communication standard -- communication protocol
Carrier period, electrical speed, carrier period variation
STM32 virtualization environment of QEMU
随机推荐
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Hardware 1 -- relationship between gain and magnification
Nb-iot technical summary
Talk about the function of magnetic beads in circuits
MySQL MHA high availability cluster
QEMU STM32 vscode debugging environment configuration
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
Array integration initialization (C language)
实例010:给人看的时间
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
实例008:九九乘法表
Communication standard -- communication protocol
Stm32--- systick timer
Use indent to format code
Shell script
UE像素流,来颗“减肥药”吧!
STM32 --- serial port communication
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
PMSM dead time compensation
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off