【LeetCode练习题】Reverse Words in a String
Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".Clarification:
- What constitutes a word?
A sequence of non-space characters constitutes a word.- Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.- How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
反转string里面的单词顺序。
注意在Clarification里头:
一个单词中不包含空格,字符串开始和结尾可以有若干个空格符号,单词与单词之间的空格可以有一个或多个空格符号,我们需要把他变成一个空格。
解题思路:
我是这样想的,把s中的每一个单词都分割出来存储到一个vector里面去,再让这个vector从后往前遍历,result字符串就是题目中要求的格式了,然后赋值给s就行了。
首先,如何去掉字符串s首尾的空格呢?
通过string的find_first_not_of()和find_last_not_of()方法,分别用begin和end指向第一个单词的开始和最后一个单词的结尾处,接下来,只需要在begin和end范围里进行for循环就行了。
那~怎么判断我当前的 i 是在单词里面还是在单词外面呢?
设置一个bool 变量 betweenWord,当betweenWord为true表示在单词内部,betweenWord为false时在单词外面。
当从单词内部到下一个空格字符时,通过substr获得第一个单词,然后设置betweenWord为false,当下一个字符不是空格时候,说明进入了单词内部了,用cur指向该单词的第一个位置,然后继续遍历直到下一个空格处,说明不在单词内部了,我们获得另一个sub单词,存进vector。
最后,用result字符串保存符合格式的字符串,再赋值给s。
代码如下:
class Solution {
public:
void reverseWords(string &s) {
size_t begin = s.find_first_not_of(" ");
if(begin == string::npos){
//s中全都是空格符号
s = "";
return ;
}
size_t end = s.find_last_not_of(" ");
bool betweenWord = true;
string sub,result;
size_t cur = begin;
vector<string> vec; for(int i = begin; i <= end; i++){
if(betweenWord){
if(s[i] == ' ' || i == end){
//进入到单词外部
if(i == end){
sub = s.substr(cur,i-cur+);
vec.push_back(sub);
}
else{
sub = s.substr(cur,i-cur);
vec.push_back(sub);
}
betweenWord = false;
}
}
else{
if(s[i] != ' '){
//进入单词内部
if(i == end){
//最后一个单词只有一个字符的情况
sub = s.substr(i,);
vec.push_back(sub);
}
cur = i;
betweenWord = true;
}
}
} for(int i = vec.size()-; i > ; i--){
result += vec[i] + " ";
}
result += vec[]; s = result;
}
};
哎,我写了那么多,别人用十几行就搞定了,这就是差距啊……
Orz……
void reverseWords(string &s)
{
string rs;
for (int i = s.length()-; i >= ; )
{
while (i >= && s[i] == ' ') i--;
if (i < ) break;
if (!rs.empty()) rs.push_back(' ');
string t;
while (i >= && s[i] != ' ') t.push_back(s[i--]);
reverse(t.begin(), t.end());
rs.append(t);
}
s = rs;
}
【LeetCode练习题】Reverse Words in a String的更多相关文章
- [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& ...
- [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 ...
- [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 ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 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 ...
- 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 ...
- 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 ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- 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 ...
随机推荐
- 【CF 189A Cut Ribbon】dp
题目链接:http://codeforces.com/problemset/problem/189/A 题意:一个长度为n的纸带,允许切割若干次,每次切下的长度只能是{a, b, c}之一.问最多能切 ...
- hdu 5612 Baby Ming and Matrix games(dfs暴力)
Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- JavaScript 基础二
JavaScript 事件处理程序就是一组语句,在事件(如点击鼠标或移动鼠标等)发生时执行 ●当事件之间互相影响时,需要有个先后顺序,这时我们声明一个Bool值来做约束 浏览对象: window 对象 ...
- pyqt labe界面超级链接例子学习
def bz(self): self.lable1=QtGui.QLabel(u'<br><a href=http://windows.microsoft.com/zh-cn/win ...
- Tcp通讯协议
了解了Udp通讯协议之后,我们再认识一个常用的通讯协议:Tcp Tcp传输特点: --依赖于Socket和ServerSocket对象 --建立客户端和服务端 --建立连接后,通过Socket中的 I ...
- [转]XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks
感谢: XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks XNOR-Net ImageNet Cl ...
- [Hapi.js] View engines
View engines, or template engines, allow you to maintain a clean separation between your presentatio ...
- 解开Android应用程序组件Activity的"singleTask"之谜
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6714543 在Android应用程序中,可以配 ...
- 07_DICTIONARY_ACCESSIBILITY
07_DICTIONARY_ACCESSIBILITY 控制对系统权限的限制: TRUE 有相应系统权限,允许访问SYS下的对象. FALSE 确保拥有可以访问任何对象的系统权限,但不可以访问SYS下 ...