当前位置:网站首页>力扣151. 颠倒字符串中的单词
力扣151. 颠倒字符串中的单词
2022-07-31 05:17:00 【旺仔 小馒头】
给你一个字符串 s ,颠倒字符串中 单词 的顺序。
单词 是由非空格字符组成的字符串,s 中使用至少一个空格将字符串中的 单词 分隔开。
返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。
注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
示例 1:
输入:s = "the sky is blue"
输出:"blue is sky the"
示例 2:
输入:s = " hello world "
输出:"world hello"
解释:颠倒后的字符串中不能存在前导空格和尾随空格。
示例 3:
输入:s = "a good example"
输出:"example good a"
解释:如果两个单词间有多余的空格,颠倒后的字符串需要将单词间的空格减少到仅有一个。
算法思路:
从后向前,不停的把单词添加到 res 中,最终返回结果。
关键点:
1.如何修剪掉两端空格;
2.如何把单词反转过来;
3.如何跳过中间连续的空格。

如何修剪掉两端空格
既然是两端的空格,那么肯定要有左右两个剪刀️,也就是 left和 right双指针

int left = 0;
int right = s.size() - 1;
// 清除左边的空格
while (s[left] == ' ') {
left++;
}
// 清除右边的空格
while (s[right] == ' ') {
right--;
}left指针向右修剪,right指针向左修剪,把空格都减掉,
然后,我们迎来来第一个想要的单词 LOVER。

我们想要把它装在 res 里面
如何把单词反转过来
我们想要拿到 LOVER,自然要知道 LOVER的范围,也就是知道L在哪里,R在哪里。
于是我们用一个 index指针向左移动,有空格时候停止,自然就圈定了 LOVER的范围。
在从(index,right] (左开右闭区间)中遍历一下字符添加到 res 中就好了

int index = right;
while (index >= left && s[index] != ' ')
index--;
// 现在 index 已经找到第一个空格,i = index+1 后移到字符串出现的位置,添加字符串
res += s.substr(index + 1 ,right - index);如何跳过中间连续的空格
还是用 index当做排头兵, index指针向左移动,有字符时候停止,

然后 right跟进


class Solution {
public:
string reverseWords(string s) {
int left = 0;
int right = s.size() - 1;
// 清除字符串两边的空格
// 清除左边
while(left < s.size() && s[left]==' '){
left++;
}
// 清除右边
while(right > 0 && s[right]==' ' ){
right--;
}
// 此时left和right都已指向前后单词的第一个字符了,创建一个string容器用来存放最后的结果
string res;
while (left <= right){
// 用index索引来判断一个单词是否结束,从后往前将单词压入res,
int index = right;
// 向左遍历找第一个空格
while (s[index] != ' ') index--;
// 现在 index 已经找到第一个空格,i=index+1 后移到字符串出现的位置,添加字符串
for (int i = index+1; i <= right; i++)
res.push_back(s[i]);
// 如果不是最后一个单词,就添加空格
if(index > left) res.push_back(' ');
// 使用 index 指针跳过中间可能出现的空格,找到下一个新单词的末尾
while(index>=left && s[index] == ' '){
index--;
}
// 把right放到下一个单词出现的位置,继续循环
right = index;
}
return res;
}
};边栏推荐
- Virtual machine view port number process
- 计算图像数据集均值和方差
- mPEG-DMPE Methoxy-polyethylene glycol-bismyristyl phosphatidylethanolamine for stealth liposome formation
- 2022 SQL big factory high-frequency practical interview questions (detailed analysis)
- ImportError: cannot import name ‘Xxxx‘ from partially initialized module ‘xx.xx.xx‘
- 用pytorch里的children方法自定义网络
- MySQL master-slave switching steps
- DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用
- MySQL 免安装版的下载与配置教程
- UR3机器人雅克比矩阵
猜你喜欢

Log jar package conflict, and its solution

MySQL 主从切换步骤

【Rhapsody学习笔记】3:Dishwasher

The solution to the IDEA console not being able to enter information

wangeditor富文本编辑器上传图片以及跨域问题解决

十分钟教你玩转分支语句!!!!!小白速进,新手福利!!

CAS:1403744-37-5 DSPE-PEG-FA 科研实验用磷脂-聚乙二醇-叶酸

变分自编码器VAE实现MNIST数据集生成by Pytorch

【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境

科研试剂Cholesterol-PEG-Maleimide,CLS-PEG-MAL,胆固醇-聚乙二醇-马来酰亚胺
随机推荐
Natural language processing related list
opencv之访问图像像素的三种方法
Wangeditor rich text editor to upload pictures and solve cross-domain problems
2021-09-30
Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
实现离线文件推流成rtsp 2
词向量——demo
二进制转换成十六进制、位运算、结构体
cocos2d-x-3.2 create project method
cocos2d-x implements cross-platform directory traversal
Hyperparameter Optimization - Excerpt
np.fliplr与np.flipud
mPEG-DMPE 甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺用于形成隐形脂质体
Rejection sampling note
学习JDBC之获取数据库连接的方式
Data Preprocessing, Feature Engineering, and Feature Learning - Excerpt
pytorch学习笔记10——卷积神经网络详解及mnist数据集多分类任务应用
random.randint函数用法
解决background-size:cover时图片铺满但显示不完整?
DingTalk Enterprise Internal-H5 Micro Application Development