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. HBase的安装与部署

    一.部署前置环境 先部署分布式的高可用版的Hadoop,即ZooKeeper+Hadoop. https://www.cnblogs.com/live41/p/15483192.html * 部署的服 ...

  2. 第35篇-方法调用指令之invokespecial与invokestatic

    这一篇将详细介绍invokespecial和invokestatic字节码指令的汇编实现逻辑 1.invokespecial指令 invokespecial指令的模板定义如下: def(Bytecod ...

  3. 一维前缀和 连续数组和为k

    给定一个整数数组和一个整数 k ,请找到该数组中和为 k 的连续子数组的个数. 滑动窗口没办法解决有负数的情况 方法一: 预处理 前缀和 sum_ij = preSum[j] - preSum[i-1 ...

  4. 【JavaScript实用技巧(二)】Js操作DOM(由问题引发的文章改版,新人大佬都可)

    [JavaScript实用技巧(二)]Js操作DOM(由问题引发的文章改版,新人大佬都可!) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人 ...

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

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

  6. CCCC-exercise

    CCCC-exercise 1.L1 总结L1 1-27里面我觉得有东西可以总结的题目 贴了部分的代码 L1-006(20) 一个正整数 N 的因子中可能存在若干连续的数字.例如 630 可以分解为 ...

  7. 面向政务企业的开发者工具集-逐浪文本大师v0.1正式发布(含代码全部开源啦!)

    这是一款基于.net 4.7环境开发的开发者工具. 一个实用的windows小工具集合,里面包含了多个常用的小软件.其中的批量修改文件名及文件内容功能,可以自定义修改规则,支持规则的导入与导出.不需要 ...

  8. selenium基本使用,及cannot find chrome binary解决方案

    什么是selenium? Selenium是一个用于Web应用程序测试的工具. Selenium 测试直接运行在浏览器中,就像真正的用户在操作一样. 支持通过各种driver(FirfoxDriver ...

  9. python 字符串和demical转换

    转成decimal格式 import decimal a="12" #判断是否是有数字 print(a.isdecimal()) #转化成decimal.Decimal格式 a1= ...

  10. php多域名跳转nginx

    当web服务器nginx已经配置好了网站根目录时,需要增加另外的域名.但是由于限制必须在原来的网站根目录下面,nginx已经无法实现.只能通过php index页面进行调试.如下面: define(' ...