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. logstash的mutate过滤器的使用

    logstash的mutate过滤器的使用 一.背景 二.需求 三.实现步骤 1.安装 `csv codec` 插件 2.准备需要读取的文件数据 3.编写 pipeline ,读取和输出数据 4.mu ...

  2. Prometheus基于文件的服务发现

    Prometheus基于文件的服务发现 一.基于文件的服务发现 1.prometheus.yml 配置文件的写法 2.file_sd 目录下的文件 3.配置结果 二.注意事项 三.参考链接 一.基于文 ...

  3. NB-IoT的DRX、eDRX、PSM三个模式怎么用?通俗解释,看完就懂!

    面我们讲了不少NB-IOT的应用.软件和硬件设计的变动. (链接在文章末尾). 今天讲讲NB-IoT的三大模式,在各种物联网和智能硬件场景中的使用方法 DRX.eDRx.PSM是什么? DRX虽然叫做 ...

  4. 21.10.9 test

    T1 购票方案 \(\color{green}{100}\) 对于每个时间节点维护它作为每种票所能包含的最后一个点时,这种票的起始点位置,由于这个位置是单调的,所以类似双指针维护,\(O(KN)\) ...

  5. Win powershell执行策略配置

    参考连接:https://blog.csdn.net/jeffxu_lib/article/details/84710386 参考连接:http://www.cragsman.org/index.ph ...

  6. js 基本用法和语法

    js 基础用法 点击事件     <!-- 第一种点击事件方式 -->   <!-- <div class="div" onclick="aler ...

  7. Jmeter分布式 (三)

    一.什么是分布式测试 分布式测试是指通过局域网和Internet,把分布于不同地点.独立完成特定功能的测试计算机连接起来,以达到测试资源共享.分散操作.集中管理.协同工作.负载均衡.测试过程监控等目的 ...

  8. MySQL高级篇 | 分析sql性能

    在应用的的开发过程中,由于初期数据量小,开发人员写 SQL 语句时更重视功能上的实现,但是当应用系统正式上线后,随着生产数据量的急剧增长,很多 SQL 语句开始逐渐显露出性能问题,对生产的影响也越来越 ...

  9. pipeline学习

    目录 一.常用语法 二.基础使用 三.使用 Groovy 沙盒 四.参数化构建过程 五.pipeline script from SCM 六.参考 一.常用语法 1.拉取git仓库代码 checkou ...

  10. vue+node+mongondb实战之路由

    看了一段时间vue的文档,一直没有机会来开发一个真正的vue项目,趁着这几天清闲,整合一下最新的技术,变学变练来开发一个vue的简单博客 有了开发博客的想法之后,谁知道第一步就被拦住了,看了vue的基 ...