当前位置:网站首页>LeetCode151-颠倒字符串中的单词-字符串-模拟
LeetCode151-颠倒字符串中的单词-字符串-模拟
2022-04-23 14:48:00 【李烦烦搞快点】
Note:
先翻转一下子,然后把每个单词抠出来,在翻转一下放到答案里面
好像也可以不开新的string 直接在原来的上面操作
class Solution {
public:
string reverseWords(string s) {
string ans;
reverse(s.begin(), s.end());
int p = 0;
while(p < s.size()){
string word = "";
while(s[p] == ' ' && p < s.size()) p ++;
while(s[p] != ' ' && p < s.size()) word += s[p ++];
reverse(word.begin(), word.end());
ans += word;
ans += ' ';
}
ans.pop_back();
if(ans[ans.size() - 1] == ' ') ans.pop_back();
return ans;
}
};
版权声明
本文为[李烦烦搞快点]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Mr_Ghost812/article/details/124350534
边栏推荐
- Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
- How do I open the win10 startup folder?
- 8.5 循环神经网络简洁实现
- 解决computed属性与input的blur事件冲突问题
- Matrix exchange row and column
- QT Detailed explanation of pro file
- Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
- Vscode Chinese plug-in doesn't work. Problem solving
- 1 - first knowledge of go language
- Want to be an architect? Tamping the foundation is the most important
猜你喜欢
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
MySQL error packet out of order
MDS55-16-ASEMI整流模块MDS55-16
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
你還不知道責任鏈模式的使用場景嗎?
想要成为架构师?夯实基础最重要
ASEMI三相整流桥和单相整流桥的详细对比
The art of automation
3、 Gradient descent solution θ
随机推荐
Progress in the treatment of depression
Chapter 7 of JVM series -- bytecode execution engine
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
pnpm安装使用
select 同时接收普通数据 和 带外数据
8.4 循环神经网络从零实现
MySQL error packet out of order
3、 Gradient descent solution θ
ASEMI超快恢复二极管与肖特基二极管可以互换吗
【无标题】
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
科技的成就(二十一)
redis的五种数据类型
【Servlet】Servlet 详解(使用+原理)
Explain TCP's three handshakes in detail
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
Don't you know the usage scenario of the responsibility chain model?
全连接层的作用是什么?
2-GO variable operation