当前位置:网站首页>Sword finger offer 05. replace spaces
Sword finger offer 05. replace spaces
2022-07-25 05:28:00 【A wise man should not be bald】
Please implement a function , Put the string s Replace each space in with "%20".

Method 1 : Apply for extra space
class Solution {
public:
// thought : Create a new string as the returned result , Traversal string , If you encounter ' ', Just add “%20”, If the ' ', Append the traversed characters
string replaceSpace(string s) {
string res = "";
for(auto ch : s){
if(ch == ' '){
res += "%20";
}
else{
res += ch;
}
}
return res;
}
};
// Time complexity :O(N) Traversal string .
// Spatial complexity :O(N) A new string Extra space for type .Method 2 : Modify in place
class Solution {
public:
//1. initialization : The number of spaces count , character string s The length of len ;
//2. Count the number of spaces : Traverse s , In case of space count++ ;
//3. modify s length : After adding "%20" The length of the string after should be len + 2 * count ;
//4. Traversal modification in reverse order :i Point to the element at the end of the original string , j Point to the element at the end of the new string ; When i = j Jump out when ( Represents that there is no space on the left , There is no need to continue traversing );
// When s[i] When not a space : perform s[j] = s[i] ;
// When s[i] When is a space : Close the string [j-2, j] The element of is changed to "%20" ; Because of the modification 3 Elements , Therefore need j -= 2 ;
//5. Return value : Modified string s ;
string replaceSpace(string s) {
int count = 0; // Used to count the number of spaces
int len = s.size();
// Count the number of spaces in the string
for(char c : s) {
if(c == ' ') {
count++;
}
}
// Modify the length of the original string
s.resize(len + 2 * count);
// Traversal modification in reverse order
// use i To traverse the original string in reverse order , use j To traverse the expanded string in reverse order
for(int i = len - 1, j = s.size() - 1; i < j; i--,j--) {
if(s[i] != ' '){
s[j] = s[i];
}
else{
s[j - 2] = '%';
s[j - 1] = '2';
s[j] = '0';
j -= 2;
}
}
return s;
}
// Time complexity O(N) : Traversal Statistics 、 Traversal and modification all use O(N) Time .
// Spatial complexity O(1) : Because it's in situ expansion s length , Therefore use O(1) Extra space .
};
边栏推荐
- background
- Application of hard coding and streaming integration scheme based on spice protocol in cloud games
- JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
- ThreadLocal
- flex布局常用属性总结
- Redis cluster setup (Windows)
- deep报错
- SystemVerilog中$write与$display区别
- 自己实现is_base_of
- Nexttick principle analysis
猜你喜欢
随机推荐
ZTE's first 5g mobile phone, axon 10 pro, was launched in the first half of this year
Tips for downloading videos in batches
Obj file format and.Mtl file format
What should testers do if they encounter a bug that is difficult to reproduce?
Implementation principle of epoll
Sword finger offer special shock edition day 9
Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
systemVerilog中automatic用法
JWT(json web token)
Introduction to kubernetes
STL notes (I): knowledge system
Game 302 of leetcode
uniapp手机端uView的u-collapse组件高度init
Use getifaddrs to obtain the IP address of the local network interface
LCP插件创建对等VLAN接口
单点登录(一处登录,处处可用)
Special analysis of data security construction in banking industry
项目管理工具——阿里云Projex介绍与实战
06. Libavdevice Library of ffmpeg
The third day of rhcsa summer vacation









