当前位置:网站首页>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
边栏推荐
- QT actual combat: Yunxi calendar
- Epolloneshot event of epoll -- instance program
- Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
- On the insecurity of using scanf in VS
- Master in minutes --- ternary operator (ternary operator)
- select 同时接收普通数据 和 带外数据
- 51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- 编程哲学——自动加载、依赖注入与控制反转
- DVWA之暴力破解(Brute Force)Low-->high
- 冰冰学习笔记:一步一步带你实现顺序表
猜你喜欢
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
PCIe X1 插槽的主要用途是什么?
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
8.5 循环神经网络简洁实现
Thread synchronization, life cycle
eolink 如何助力遠程辦公
数组模拟队列进阶版本——环形队列(真正意义上的排队)
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
随机推荐
线程同步、生命周期
1n5408-asemi rectifier diode
select 同时接收普通数据 和 带外数据
ASEMI三相整流桥和单相整流桥的详细对比
8.3 语言模型与数据集
解决computed属性与input的blur事件冲突问题
【Proteus仿真】自动量程(范围<10V)切换数字电压表
【Servlet】Servlet 详解(使用+原理)
UML项目实例——抖音的UML图描述
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
Contraction mapping theorem
GIS数据处理-cesium中模型位置设置
eolink 如何助力远程办公
一款不错的工具:aardio
8.2 文本预处理
Thread synchronization, life cycle
redis的五种数据类型
Epoll's et, lt working mode -- example program
详解TCP的三次握手
Svn detailed use tutorial