class Solution {
public:
void reverseWords(string &s) {
vector<string> data;
string word;
stringstream ss(s);
while(ss>>word) data.push_back(word); vector<string> rdata(data.rbegin(), data.rend()); s = accumulate(rdata.begin(), rdata.end(), string(""),
[](string s1, string s2){
if(s1.empty()) return s2;
else return s1+" "+s2;
}); }
};

class Solution {
public:
void reverseWords(string &s) {
for(string::size_type ix=0;ix!=s.size();++ix)
{
if(s[ix]==' ')
if(s[ix+1]==' '){
s.erase(ix,1);
ix--;
}
}
if(s[0]==' ')s.erase(0,1);
if(s[s.size()-1]==' ')s.erase(s.size()-1,1);
int k=0;
string st(s.size(),'a');
for(string::size_type ix=s.size()-1;ix!=-1;--ix)//全倒置
{
st[k++]=s[ix];
}
int beg=0,end=0,n=st.size();
int index=0;
while(end<=n)
{
while(st[end]!=' '&&end<n)end++;
for(int i=end-1;i>=beg;--i)s[index++]=st[i];
if(st[end]==' ')s[index++]=' ';
end=end+1;
beg=end;
}
}
};

4.Reverse Words in a String-Leetcode的更多相关文章

  1. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  2. Reverse Words in a String——LeetCode

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  3. Reverse Words in a String leetcode

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  4. Reverse Words in a String leetcode java

    题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...

  5. Reverse Words in a String | LeetCode OJ | C++

    我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...

  6. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  7. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  8. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  9. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  10. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

随机推荐

  1. netty传输java bean对象

    在上一篇博客(netty入门实现简单的echo程序)中,我们知道了如何使用netty发送一个简单的消息,但是这远远是不够的.在这篇博客中,我们来使用netty发送一个java bean对象的消息,但是 ...

  2. Machine learning(4-Linear Regression with multiple variables )

    1.Multiple features So what the form of the hypothesis should be ? For convenience, define x0=1 At t ...

  3. lvs 四层负载相关

    都打开 /etc/sysctl.conf 中的 net.ip4.ip_forward=1.开启路由转发功能. 分发器 : eth0:192.168.1.66 (VIP) eth1:192.168.2. ...

  4. Python学习路线【对标大厂Python工程师的招聘要求,并推荐优质免费资源】打卡学习不迷茫

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文要点:从Python爬虫工程师的招聘要求出发制定学习路线,同时还推荐免费优质的学习资源. 打卡学习不迷茫. 干货满满,建议收藏,需要用到时常看 ...

  5. Qt Creator 常用快捷键 详细总结

    下面是我总结的一些Qt Creator 常用快捷键 ,可以大大提高我们使用Qt开发项目的效率!! Qt Creator 常用快捷键 快捷键 介绍 F1 查看帮助文档 Shift + F2 函数的声明和 ...

  6. Netty数据如何在 pipeline 中流动

    前言 在之前文章中,我们已经了解了pipeline在netty中所处的角色,像是一条流水线,控制着字节流的读写,本文,我们在这个基础上继续深挖pipeline在事件传播 Unsafe对象 顾名思义,u ...

  7. hdfs command

    hadoop fs -ls hdfs dfs -mkdir -p /user/$(whoami) hdfs dfs -chown -R $(whoami) /user/$(whoami) hdfs d ...

  8. Mysql - 如何决定用 datetime、timestamp、int 哪种类型存储时间戳?

    背景 数据表都很可能会有一两个字段需要保存日期时间数据,那应该用什么 Mysql 类型来保存呢? 前面讲过 datetime.timestamp.int 的方式来保存日期时间 如何存储 10位.13位 ...

  9. 大一C语言学习笔记(10)---编程篇--制作简易计算器,支持加,减,乘,除,取余运算,要求 0 bug

    博主自开学初就一直在努力为自己的未来寻找学习方向,学习编程嘛,尽量还是要抱大腿的,所以我就加入了我们学校的智能设备研究所,别的不说,那的学长们看起来是真的很靠谱,学长们的学习氛围也超级浓厚,所以我就打 ...

  10. 菜鸡的Java笔记 第三十 - java 异常的捕获及处理

    异常的捕获及处理        1.异常的产生分析以及所带来的影响        2.异常的处理的基本格式        3.异常的处理流程        4.异常的处理模式        5.自定义 ...