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. 主仆见证了 Hobo 的离别 题解

    前言: 题面挺神仙.反正我考试的时候看了40分钟也没看懂. 后来改题感觉自己写的挺假,没想到加个\(k==1\)的特判竟然就A了?无语力. 解析: 看懂题以后就好说了.首先这显然是一个树形结构.我们考 ...

  2. Linux多线程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  3. Python爬取COVID-19疫情监控实战

    一.项目概述 本项目基于Python.Flask.Echarts打造的一个疫情监控系统,涉及技术: Python网络爬虫 Python与Mysql数据库交互 使用Flask构建web项目 基于Echa ...

  4. Xpath运算符

    5.position定位 >>print tree.xpath('//*[@id="testid"]/ol/li[position()=2]/text()')[0] & ...

  5. Appium 介绍与环境搭建

    目录 Appium 介绍 APP 自动化测试介绍 什么是 Appium ? Appium 优势 Appium 架构 Appium 生态 Appium 组件 UiAutomator API Bootst ...

  6. jQuery淡入淡出效果

    如果是通过鼠标点击事件来触发动画效果可以使用 $("#button").click(function(){ $("#div").stop().fadeToggl ...

  7. JMeter学习笔记--录制脚本(二)

    第一步:在JMeter中添加线程组,命名为访问首页 第二步:在线程组下添加HTTP请求默认值 添加->配置元件->HTTP请求默认值,设置服务器IP和端口号(JMeter默认使用80端口号 ...

  8. LeetCode 22. 括号生成 C++(回溯法)

      还是用回溯法暴力解题,遍历所有可能,不过还是在此基础上进行了一些的优化,来阻止那些不必要的遍历.好,上代码. class Solution { public: vector<string&g ...

  9. centos7.2安装rabbitmq教程

    环境: centos7.2 rabbitmq依赖erlang,需要先安装erlang 1 安装erlang rpm -Uvh https://download.fedoraproject.org/pu ...

  10. Salesforce Consumer Goods Cloud 浅谈篇三之 行动计划(Action Plan)相关配置

    本篇参考: https://v.qq.com/x/page/f0772toebhd.html https://v.qq.com/x/page/e0772tsmtek.html https://v.qq ...