当前位置:网站首页>Leetcode - Sword finger offer 05 Replace spaces
Leetcode - Sword finger offer 05 Replace spaces
2022-07-07 14:19:00 【styfish】
summary
The finger of the sword Offer 05. Replace blank space
analysis
This question , If the space complexity is not limited , It's very simple , Just open a new one
stringIt can be realizedhowever , If the request can only be handled in principle ? There is a problem to be solved :
s --> %20, Added characters , Need to increase the striing The capacity ofbecause
push_backIt can be the last operation
Ideas
Add capacity
- You can traverse it first string, Count the number of spaces , You can know the final size that needs to be expanded
- And then use it
resize()To expand the size
Code
class Solution {
public:
string replaceSpace(string s) {
int count = 0; // Count the number of spaces
int sOldSize = s.size();
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
count++;
}
}
// Extended string s Size , That is, replace each space with "%20" After the size of
s.resize(s.size() + count * 2);
// In order to operate in situ , So use double finger acupuncture
int ptr = s.size() - 1, wokr_ptr = sOldSize - 1;
// This is from the back to the front , Because if you add new elements from front to back , Will overwrite the original elements (
for(; wokr_ptr >= 0; --wokr_ptr) {
if (s[wokr_ptr] != ' ') s[ptr--] = s[wokr_ptr];
else {
s[ptr] = '0';
s[ptr - 1] = '2';
s[ptr - 2] = '%';
ptr -= 3;
}
}
return s;
}
};
边栏推荐
- Selenium Library
- MRS离线数据分析:通过Flink作业处理OBS数据
- 常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
- wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
- C # switch pages through frame and page
- Arm cortex-a9, mcimx6u7cvm08ad processor application
- call undefined function openssl_ cipher_ iv_ length
- 高等數學---第八章多元函數微分學1
- CSMA/CD 载波监听多点接入/碰撞检测协议
- Cesium knows the longitude and latitude of one point and the distance to find the longitude and latitude of another point
猜你喜欢

AI talent cultivation new ideas, this live broadcast has what you care about

常用数字信号编码之反向不归零码码、曼彻斯特编码、差分曼彻斯特编码

The longest ascending subsequence model acwing 1014 Mountaineering

UML state diagram

Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning

gvim【三】【_vimrc配置】

Did login metamask

Horizontal of libsgm_ path_ Interpretation of aggregation program

OAuth 2.0 + JWT protect API security
![[Reading stereo matching papers] [III] ints](/img/d3/4238432492ac3dc4ec14a971b8848d.png)
[Reading stereo matching papers] [III] ints
随机推荐
Vmware 与主机之间传输文件
UML state diagram
c#利用 TCP 协议建立连接
Is it safe to open an account online now? Which securities company should I choose to open an account online?
Mrs offline data analysis: process OBS data through Flink job
请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
Huawei image address
数据流图,数据字典
SAKT方法部分介绍
wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
libSGM的horizontal_path_aggregation程序解读
手把手教会:XML建模
Pert diagram (engineering network diagram)
Hands on Teaching: XML modeling
通过 iValueConverter 给datagrid 的背景颜色 动态赋值
Did login metamask
C # switch pages through frame and page
[Reading stereo matching papers] [III] ints
Search engine interface
call undefined function openssl_cipher_iv_length