当前位置:网站首页>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;
}
};
边栏推荐
- How can the PC page call QQ for online chat?
- C # use TCP protocol to establish connection
- ndk初学习(一)
- Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
- Hands on Teaching: XML modeling
- Regular expression integer positive integer some basic expressions
- Redis 核心数据结构 & Redis 6 新特性详
- Leetcode——236. 二叉树的最近公共祖先
- VSCode 配置使用 PyLint 语法检查器
- 内存溢出和内存泄漏的区别
猜你喜欢
Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
Transferring files between VMware and host
最长上升子序列模型 AcWing 1014. 登山
AI talent cultivation new ideas, this live broadcast has what you care about
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
The longest ascending subsequence model acwing 1012 Sister cities
Pert diagram (engineering network diagram)
Use day JS let time (displayed as minutes, hours, days, months, and so on)
JS get the current time, month, day, year, and the uniapp location applet opens the map to select the location
随机推荐
最长上升子序列模型 AcWing 1012. 友好城市
带你掌握三层架构(建议收藏)
Cascading update with Oracle trigger
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
OAuth 2.0 + JWT protect API security
PC端页面如何调用QQ进行在线聊天?
Excuse me, why is it that there are no consumption messages in redis and they are all piled up in redis? Cerely is used.
Introduction to sakt method
Is it safe to open an account online now? Which securities company should I choose to open an account online?
高等數學---第八章多元函數微分學1
The meaning of variables starting with underscores in PHP
Use case diagram
Interface automation test - solution of data dependency between interfaces
Environment configuration
Hands on Teaching: XML modeling
UML state diagram
Hangdian oj2054 a = = B? ???
接口自动化测试-接口间数据依赖问题解决
[network security] SQL injection syntax summary
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )