当前位置:网站首页>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;
}
};
边栏推荐
- c#利用 TCP 协议建立连接
- The meaning of variables starting with underscores in PHP
- Csma/cd carrier monitoring multipoint access / collision detection protocol
- Environment configuration
- Did login metamask
- Use case diagram
- C # use TCP protocol to establish connection
- Redis can only cache? Too out!
- Attribute keywords aliases, calculated, cardinality, ClientName
- wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
猜你喜欢
常用数字信号编码之反向不归零码码、曼彻斯特编码、差分曼彻斯特编码
Selenium Library
手把手教会:XML建模
GVIM [III] [u vimrc configuration]
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
Introduction to sakt method
最长上升子序列模型 AcWing 1014. 登山
Use day JS let time (displayed as minutes, hours, days, months, and so on)
The longest ascending subsequence model acwing 1012 Sister cities
Data flow diagram, data dictionary
随机推荐
Hands on Teaching: XML modeling
libSGM的horizontal_path_aggregation程序解读
Excuse me, does PTS have a good plan for database pressure measurement?
Lavarel之环境配置 .env
Laravel Form-builder使用
用例图
Leetcode——剑指 Offer 05. 替换空格
THINKPHP框架的优秀开源系统推荐
Attribute keywords aliases, calculated, cardinality, ClientName
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
Take you to master the three-tier architecture (recommended Collection)
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
手把手教会:XML建模
Environment configuration
Leetcode——236. 二叉树的最近公共祖先
Cascading update with Oracle trigger
FCOS3D label assignment
Environment configuration of lavarel env
Redis 核心数据结构 & Redis 6 新特性详