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. [BZOJ2331]地板(插头DP)

    Description lxhgww的小名叫"小L",这是因为他总是很喜欢L型的东西.小L家的客厅是一个的矩形,现在他想用L型的地板来铺满整个客厅,客厅里有些位置有柱子,不能铺地板 ...

  2. requests中文页面乱码解决方案【转】

    requests中文页面乱码解决方案!   请给作者点赞 --> 原文链接 Python中文乱码,是一个很大的坑,自己不知道在这里遇到多少问题了.还好通过自己不断的总结,现在遇到乱码的情况越来越 ...

  3. EF上下文对象创建之线程内唯一

    在一次请求中,即一个线程内,若是用到EF数据上下文对象,就创建一个,那么会造成数据混乱,每次创建的对象执行相应的数据库操作,此同时,其他的EF对象内获得的数据可能已经是“过期”的了.即这个数据已经变动 ...

  4. TCP/IP网络编程之域名及网络地址

    域名系统 DNS是对IP地址和域名进行互相转换的系统,其核心是DNS服务器.提供网络服务的服务端也是通过IP地址来区分的,但由于IP地址难于记忆,因此通过容易记忆并表述的域名来取代IP地址 在浏览器地 ...

  5. TCP/IP网络编程之基于TCP的服务端/客户端(二)

    回声客户端问题 上一章TCP/IP网络编程之基于TCP的服务端/客户端(一)中,我们解释了回声客户端所存在的问题,那么单单是客户端的问题,服务端没有任何问题?是的,服务端没有问题,现在先让我们回顾下服 ...

  6. Windows下Redis3.2.10及图像化工具redis-desktop-manager安装教程

    1.下载地址: GitHub地址:https://github-production-release-asset-2e65be.s3.amazonaws.com/3402186/bb1d10fc-3f ...

  7. Leetcode 645.最长数对链

    最长数对链 给出 n 个数对. 在每一个数对中,第一个数字总是比第二个数字小. 现在,我们定义一种跟随关系,当且仅当 b < c 时,数对(c, d) 才可以跟在 (a, b) 后面.我们用这种 ...

  8. 删除 node 【笔记】

    sudo npm uninstall npm -g sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receip ...

  9. 【转】Unity3D研究院之两种方式播放游戏视频

    http://www.xuanyusong.com/archives/1019   Unity3D中播放游戏视频的方式有两种,第一种是在游戏对象中播放,就好比在游戏世界中创建一个Plane面对象,摄像 ...

  10. iptables + Denyhost抵御暴力破解

    使用iptables 现在每分钟连接ssh的次数 #允许本地环回接口访问   1 iptables -A INPUT -i lo -j ACCEPT #对已经建立的所有链接都放行   1 iptabl ...