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.sort()进行排序. 排序索引值 使用np.argsort()排序,返回排序后的索引值. 备注:array1[1,2]=1.2,array1[1,0]=5.6,array1 ...

  2. 手把手教你启用RemoteFX以及Hyper-V GPU卸载

    [TechTarget中国原创] 微软的RemoteFX特性可以帮助改善虚拟机图形密集型应用工作负载的性能. 服务器虚拟化已经成熟到大多数工作负载都能够在虚拟机内运行的程度.毫无疑问,与其他工作负载相 ...

  3. 虚拟架构就绪 | 谈谈Windows Server 2012 R2迁移这件小事

    我们所说的“新选择”包括操作系统升级——告别Windows Server 2003或2008,选择用什么样的姿势进行升级呢? 新年伊始,正是企业对自身IT基础设施进行评估的最佳时期.在多项评估项目里面 ...

  4. windows phone UI吐槽---跑偏了就再也跑不回来了

    首先wp的ui灵感来自瑞士的平面设计:      先上两张图,嗯,确实不错,简洁明了,强调的是信息本身,而不是冗余的界面元素,传达准确. 在现实生活中这种突出信息的设计语言也不时见到:    可以总结 ...

  5. python学习_常用语句

    1.变量 1 name='hu qihang' #变量名应为英文,方便阅读 2 name_of_gf='chen ye' #多个单词用下划线隔开 3 名字='hu qihang' #变量名可以是中文 ...

  6. 【转】Unity 游戏存档 PlayerPrefs类的用法

    http://www.cnblogs.com/qiaogaojian/p/5969855.html unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs.工作原理非常简单,以 ...

  7. icheck 动态设置选中,判断是否选择

    $(this).iCheck('check'); //启用禁用上级编号             $('#OnPar').on('ifUnchecked', function (event) {     ...

  8. Kafka 1.0版本发布

    Kafka 1.0版本发布 1.0.0 2017年11月1日发布 源码下载: kafka-1.0.0-src.tgz(asc,sha512) 二进制下载: Scala 2.11 - kafka_2.1 ...

  9. 【bzoj4066】简单题 KD-tree

    题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...

  10. BZOJ 3224 普通平衡树(Treap模板题)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 14301  Solved: 6208 [Submit][ ...