当前位置:网站首页>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
边栏推荐
- Resolve the conflict between computed attribute and input blur event
- Outsourcing for four years, abandoned
- MySQL error packet out of order
- 金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
- Svn detailed use tutorial
- go基础 反射
- UML project example -- UML diagram description of tiktok
- capacitance
- 想要成为架构师?夯实基础最重要
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
猜你喜欢
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
Progress in the treatment of depression
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
【NLP】HMM隐马尔可夫+维特比分词
ASEMI超快恢复二极管与肖特基二极管可以互换吗
MDS55-16-ASEMI整流模块MDS55-16
[servlet] detailed explanation of servlet (use + principle)
On the insecurity of using scanf in VS
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
随机推荐
一款不错的工具:aardio
Unity_代码方式添加绑定按钮点击事件
8.5 循环神经网络简洁实现
redis的五种数据类型
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
全连接层的作用是什么?
L'externalisation a duré quatre ans.
【无标题】
GIS数据处理-cesium中模型位置设置
What is the main purpose of PCIe X1 slot?
[untitled]
When splicing HQL, the new field does not appear in the construction method
Go basic reflection
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
ASEMI三相整流桥和单相整流桥的详细对比
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
Epolloneshot event of epoll -- instance program