当前位置:网站首页>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;
}
};
边栏推荐
- IP and long integer interchange
- oracle 触发器实现级联更新
- Is the spare money in your hand better to fry stocks or buy financial products?
- Leetcode——236. 二叉树的最近公共祖先
- 参数关键字Final,Flags,Internal,映射关键字Internal
- Cascading update with Oracle trigger
- Similarities and differences between switches and routers
- Hangdian oj2054 a = = B? ???
- Clickhouse (03) how to install and deploy Clickhouse
- 使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
猜你喜欢

Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1

Did login metamask

UML state diagram

Selenium库

AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?

最长上升子序列模型 AcWing 482. 合唱队形

Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance

SAKT方法部分介绍

UML 状态图

The longest ascending subsequence model acwing 1014 Mountaineering
随机推荐
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
Hands on Teaching: XML modeling
Leetcode——236. 二叉树的最近公共祖先
Clickhouse (03) how to install and deploy Clickhouse
Vscode configuration uses pylint syntax checker
Attribute keywords aliases, calculated, cardinality, ClientName
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
IP address home location query
Wired network IP address of VMware shared host
Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
搜索框效果的实现【每日一题】
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
NDK beginner's study (1)
接口自动化测试-接口间数据依赖问题解决
课设之百万数据文档存取
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
Excellent open source system recommendation of ThinkPHP framework
PERT图(工程网络图)
【立体匹配论文阅读】【三】INTS
Regular expression integer positive integer some basic expressions