当前位置:网站首页>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
string
It 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_back
It 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;
}
};
边栏推荐
- Cesium knows the longitude and latitude of one point and the distance to find the longitude and latitude of another point
- [Reading stereo matching papers] [III] ints
- bashrc与profile
- Transferring files between VMware and host
- 请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
- SAKT方法部分介绍
- Data flow diagram, data dictionary
- 请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
- Similarities and differences between switches and routers
- CSMA/CD 载波监听多点接入/碰撞检测协议
猜你喜欢
LeetCode 648. 单词替换
Vscode configuration uses pylint syntax checker
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
AI talent cultivation new ideas, this live broadcast has what you care about
How to check the ram and ROM usage of MCU through Keil
Hands on Teaching: XML modeling
UML 顺序图(时序图)
高等數學---第八章多元函數微分學1
Vmware共享主机的有线网络IP地址
Selenium Library
随机推荐
libSGM的horizontal_path_aggregation程序解读
docker部署oracle
Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning
bashrc与profile
Dry goods | summarize the linkage use of those vulnerability tools
THINKPHP框架的优秀开源系统推荐
Seven propagation behaviors of transactions
Excerpt from "misogyny: female disgust in Japan"
Hands on Teaching: XML modeling
[network security] SQL injection syntax summary
Clickhouse (03) how to install and deploy Clickhouse
call undefined function openssl_cipher_iv_length
搜索引擎接口
How can the PC page call QQ for online chat?
Laravel Form-builder使用
AI talent cultivation new ideas, this live broadcast has what you care about
最长上升子序列模型 AcWing 1012. 友好城市
Redis can only cache? Too out!
Hangdian oj2054 a = = B? ???
mysql ”Invalid use of null value“ 解决方法