【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 ...
随机推荐
- Inorder Successor in BST 解答
Question Given a binary search tree and a node in it, find the in-order successor of that node in th ...
- Pythoner | 你像从前一样的Python学习笔记
Pythoner | 你像从前一样的Python学习笔记 Pythoner
- python练习linux下创建路径
#coding=utf-8 import os class MakeDirectory(): def mkdir(self,path): # 去除首位空格 path=path.strip() # 去除 ...
- PHP批量审核后台
/*批量审核方法*/ function setOn_all() { if($_POST) { $p=M('news'); $data=array(); $i=0; foreach ($_POST as ...
- STS(Spring Tool Suite)建立默认的spring mvc项目
引入响应的jar包解决报错: 由于国内的网络限制,下载会较慢.使用之前可自行更换maven的镜像路径,越近越好.
- PHP性能优化学习笔记--PHP周边性能优化--来自慕课网Pangee http://www.imooc.com/learn/205
PHP一般运行于Linux服务器中,周边主要包括:Linux运行环境.文件存储.数据库.缓存.网络 常见PHP场景的开销次序: 读写内存<<读写数据库(使用内存作为缓存.异步处理)< ...
- unity3d 建树篇
今天碰到有人问这个问题,然后我经过一番折腾,找到了方法.例如以下: 有学过Unity3d的同学生都知道我们在对地形拖拉树木等表层时,其树木在我们实例执行中,它们都是能够任其他物体穿过. 这是为什么.相 ...
- Android——编译odex保护
编译过android源代码的可能试验过改动编译类型.android的初始化编译配置可參考Android--编译系统初始化设置 一.TARGET_BUILD_VARIANT=user 当选择的编译类型为 ...
- MVC 错误处理1
实例1. /// <summary> /// 错误处理 /// 404 处理 /// </summary> protected void Application_Error(o ...
- android动画效果大全
动画类型 Android的animation由四种类型组成 Android动画模式 Animation主要有两种动画模式:一种是tweened animation(渐变动画 XML中 JavaCod ...