https://oj.leetcode.com/problems/reverse-words-in-a-string/

给一个字符串 abc  dd  m,返回 m dd abc.

注意:输入中可能有前置或者后置空格,要求,都删除掉。输出的每个单词之间,空格大小为1.

class Solution {
public:
void reverseWords(string &s) {
if(s.empty())
return; //remove heading and trailing spaces
int i = ;
while(i<s.size() && s[i] == ' ')
i++;
if(i == s.size())
{
s = "";
return;
}
int j = s.size() - ;
while(j>- && s[j] == ' ')
j--;
if(j == -)
{
s = "";
return;
} s = s.substr(i,j - i + ); size_t pos = ;
vector<string> strs;
size_t begin = ;
while(begin < s.size())
{
pos = s.find_first_of(' ',begin);
if(pos == begin)
{
begin++;
continue;
}
else if(pos != -)
strs.push_back(s.substr(begin,pos - begin));
else //pos == -1, the end
{
strs.push_back(s.substr(begin,s.size() - - begin + ));
break;
}
begin = pos + ;
} string ans;
for(int i = strs.size() - ; i > ; i--)
{
ans += strs[i];
ans += " ";
}
ans += strs[]; s = ans;
}
};

LeetCode OJ-- Reverse Words in a String的更多相关文章

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

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

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

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

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

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  4. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  5. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  6. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

  7. leetcode - [1]Reverse Words in a String

    Question: Reverse Words in a String Given an input string, reverse the string word by word. For exam ...

  8. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

  9. LeetCode 345. Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels(元音字母) of a string. Example ...

  10. Python [Leetcode 345]Reverse Vowels of a String

    题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

随机推荐

  1. 科学计算库Numpy——数组生成

    等差数组 使用np.arange()或np.linspace()生成元素是等差数列的数组. 以10为底的数组 使用np.logspace()生成元素是以10为底的数组. 数组扩展 使用np.meshg ...

  2. Flask初学者:g对象,hook钩子函数

    Flask的g对象 作用:g可以可以看作是单词global的缩写,使用“from flask import g”导入,g对象的作用是保存一些在一次请求中多个地方的都需要用到的数据,这些数据可能在用到的 ...

  3. vscode添加Astyle

    1.安装astyle插件,在应用商城里面一键安装即可.2.下载astyle的bin文件,并添加到系统环境变量.3.打开vscode的settings.json,添加以下代码. { "edit ...

  4. windows服务安装卸载

    到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...

  5. 【Reverse Linked List II】cpp

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...

  6. Marketing learning-2

    Part three 1.strategies for leadership 1)operational excellence:operational competence 2)performance ...

  7. CTF 两道web整数溢出题目(猫咪银行和ltshop)

    ①猫咪银行: (2018中科大hackgame) 一开始给十个CTB,而flag需要20个CTB,我们需要理财赚够20个. 理财是只能买入TDSU才可以获得收益.我们先上来直接把CTB全部换成TDSU ...

  8. Java学习4之抽象类

    在面向父类编程的过程中,抽象出来的父类具有一般化特质.父类函数只是一个抽象化的概念,只是为了在面向对象编程时统一接口服务. example: 有时父类会定义出一些无法实现的行为: public voi ...

  9. Unity --yield return

    1.yield return null; //暂停协同程序,下一帧再继续往下执行 yield new WaitForFixedUpdate (); //暂停协同程序,等到下一次调用FixedUpdat ...

  10. 使TileCache配合OpenLayers,产生地图瓦块的一些资料(转)

    在tilecache.cfg中配置好被切割地图的参数,比如: [mytestmap]layers=3,5,7,8type=WMSurl=http://localhost/arcgis/services ...