当前位置:网站首页>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);
}
}
边栏推荐
- Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
- [trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
- Measurement fitting based on Halcon learning [i] fuse Hdev routine
- Naming rules for FreeRTOS
- Google sitemap files for rails Projects - Google sitemap files for rails projects
- go依赖注入--google开源库wire
- C # joint configuration with Halcon
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
- STM32 --- configuration of external interrupt
猜你喜欢
C # joint configuration with Halcon
[paper reading] the latest transfer ability in deep learning: a survey in 2022
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
Management and use of DokuWiki (supplementary)
Nb-iot technical summary
STM32---ADC
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Solutions to compilation warnings in Quartus II
Connection mode - bridge and net
Briefly talk about the identification protocol of mobile port -bc1.2
随机推荐
Count the number of inputs (C language)
Sizeof (function name) =?
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Shell script realizes the reading of serial port and the parsing of message
What are the test items of power battery ul2580
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Brief discussion on Buck buck circuit
Summary of SIM card circuit knowledge
STM32 --- serial port communication
Some thoughts on extracting perspectives from ealfa and Ebeta
Google sitemap files for rails Projects - Google sitemap files for rails projects
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
Use indent to format code
Slist of linked list
2020-05-21
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Several important parameters of LDO circuit design and type selection
Array integration initialization (C language)
UE像素流,来颗“减肥药”吧!