当前位置:网站首页>力扣.剑指offer05.替换空格
力扣.剑指offer05.替换空格
2022-07-31 05:17:00 【旺仔 小馒头】
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = "We are happy." 输出:"We%20are%20happy."
思想
法一:string
创建一个string容器v,遍历字符串s,遇到空格,直接push_back依次压入字符'%' , '2' ,'0',如果是普通字符直接将其压入新容器
// 法一:用C++11里的方法
class Solution1 {
public:
string replaceSpace(string s) {
string v;
for (auto ch : s)
if (ch == ' '){
v.push_back('%');
v.push_back('2');
v.push_back('0');
}
else
v.push_back(ch);
return v;
}
};
法二:replace
利用string容器自带的方法replace
string& replace(int pos, int n, const string& str); // 替换从pos开始n个字符为字符串str
// 法二:string中自带函数replace
class Solution {
public:
string replaceSpace(string s) {
// 统计空格数量
for (int i = 0; i < s.size(); i++){
if (s[i] == ' '){
s.replace(i, 1, "%20");
// 跳过已经替换后的字符%20的位置,从后遍历
i += 2;
}
}
return s;
}
};法三: 双指针法

// 法三:双指针原地更改,遇到' '就扩容2个空位,先把数组尺寸调到我们要大小
class Solution {
public:
string replaceSpace(string s) {
// 统计空格数量
int count = 0;
for (auto ch : s){
if (ch == ' '){
count++;
}
}
// 将字符串个数换成个s.size() + count*2,多的部分用' '来填
int oldSize = s.size();
int newSize = s.size() + count*2;
s.resize(newSize);
// 利用双指针,指针l指向原空间的最后一个位置,指针r指向新空间的最后一个位置,同时往前移动
int l = oldSize - 1;
int r = newSize - 1;
while (l < r){
if (s[l] == ' '){
s[r] = '0';
s[r-1] = '2';
s[r-2] = '%';
r -= 2;
}
else
s[r] = s[l];
r--;
l--;
}
return s;
}
};边栏推荐
- opencv之图像二值化处理
- 【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
- Tensorflow——演示
- 活体检测FaceBagNet阅读笔记
- Learning and understanding of ROS service programming
- Cholesterol-PEG-NHS NHS-PEG-CLS cholesterol-polyethylene glycol-active ester can modify small molecular materials
- 四种常见的POST提交数据方式
- DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用
- The solution to the IDEA console not being able to enter information
- ROS之service编程的学习和理解
猜你喜欢

Attention based ASR(LAS)

Wangeditor rich text editor to upload pictures and solve cross-domain problems

Research reagents Cholesterol-PEG-Maleimide, CLS-PEG-MAL, Cholesterol-PEG-Maleimide

日志jar包冲突,及其解决方法

wangeditor编辑器内容传至后台服务器存储

Learning and understanding of ROS service programming

【Rhapsody学习笔记】1:Hello World

2021年软件测试面试题大全

计算图像数据集均值和方差

Fluorescein-PEG-DSPE Phospholipid-Polyethylene Glycol-Fluorescein Fluorescent Phospholipid PEG Derivatives
随机推荐
CAS:474922-22-0 Maleimide-PEG-DSPE 磷脂-聚乙二醇-马来酰亚胺简述
PyTorch Study Notes 08 - Loading Datasets
自己设置的私密文件,在哪找
四种常见的POST提交数据方式
ROS之service编程的学习和理解
DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用
About iframe
Rejection sampling note
【Rhapsody学习笔记】2:Count Down
MySQL free installation download and configuration tutorial
opencv之图像二值化处理
词向量——demo
CAS:474922-22-0 Maleimide-PEG-DSPE Phospholipid-Polyethylene Glycol-Maleimide Brief Description
Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
Chemical Reagent Phospholipid-Polyethylene Glycol-Amino, DSPE-PEG-amine, CAS: 474922-26-4
科学研究用磷脂-聚乙二醇-活性酯 DSPE-PEG-NHS CAS:1445723-73-8
2022年SQL大厂高频实战面试题(详细解析)
UR3机器人运动学分析之逆运动学分析
Wangeditor rich text editor to upload pictures and solve cross-domain problems
Tensorflow边用边踩坑