当前位置:网站首页>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;
}
};
边栏推荐
- Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
- Excuse me, why is it that there are no consumption messages in redis and they are all piled up in redis? Cerely is used.
- libSGM的horizontal_path_aggregation程序解读
- GVIM [III] [u vimrc configuration]
- Is the spare money in your hand better to fry stocks or buy financial products?
- Interface automation test - solution of data dependency between interfaces
- Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
- 参数关键字Final,Flags,Internal,映射关键字Internal
- Introduction to sakt method
- Similarities and differences between switches and routers
猜你喜欢

Hands on Teaching: XML modeling

AI人才培育新思路,这场直播有你关心的

OAuth 2.0 + JWT protect API security

Mrs offline data analysis: process OBS data through Flink job

XML文件的解析操作

Selenium库

Vscode configuration uses pylint syntax checker

Redis 核心数据结构 & Redis 6 新特性详

VSCode 配置使用 PyLint 语法检查器

Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
随机推荐
Assign a dynamic value to the background color of DataGrid through ivalueconverter
PHP中用下划线开头的变量含义
The difference between memory overflow and memory leak
通过 iValueConverter 给datagrid 的背景颜色 动态赋值
Realization of search box effect [daily question]
Details of redis core data structure & new features of redis 6
Similarities and differences between switches and routers
[Reading stereo matching papers] [III] ints
Leetcode——344. 反转字符串/541. 反转字符串 II/151. 颠倒字符串中的单词/剑指 Offer 58 - II. 左旋转字符串
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
Pert diagram (engineering network diagram)
最长上升子序列模型 AcWing 482. 合唱队形
XML文件的解析操作
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
Parsing of XML files
Take you to master the three-tier architecture (recommended Collection)
股票开户首选,炒股交易开户佣金最低网上开户安全吗
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
gvim【三】【_vimrc配置】