当前位置:网站首页>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
边栏推荐
- 【Proteus仿真】自动量程(范围<10V)切换数字电压表
- 1 - first knowledge of go language
- 帧同步 实现
- QT Detailed explanation of pro file
- 8.4 循环神经网络从零实现
- select 同时接收普通数据 和 带外数据
- 多语言通信基础 06 go实现grpc的四种数据流模式实现
- Unity_ Code mode add binding button click event
- A blog allows you to learn how to write markdown on vscode
- Progress in the treatment of depression
猜你喜欢

电容
![[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection](/img/8c/e72e628a44a36bfc7406a002d00215.png)
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection

编程哲学——自动加载、依赖注入与控制反转

eolink 如何助力远程办公

【工厂模式详解】工厂方法模式

51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)

51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code

Swift - literal, literal protocol, conversion between basic data types and dictionary / array

线程同步、生命周期

Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
随机推荐
压缩映射定理
Go basic reflection
[untitled]
编程哲学——自动加载、依赖注入与控制反转
epoll 的EPOLLONESHOT 事件———实例程序
Thread synchronization, life cycle
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
《JVM系列》 第七章 -- 字节码执行引擎
1n5408-asemi rectifier diode
1-初识Go语言
电容
Contraction mapping theorem
8.2 文本预处理
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
[NLP] HMM hidden Markov + Viterbi word segmentation
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
8.5 循环神经网络简洁实现
你还不知道责任链模式的使用场景吗?
[servlet] detailed explanation of servlet (use + principle)
