151. 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
".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
click to show 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.
=====================
注意,
1,被空格包围的是单词
2,输入字符串可以以空格开头或结尾,但是结果中的字符不能以空格开头或结尾
3,输出字符串中单词间的空格是一个,不能重复出现空格.
思路:
对输入字符串进行去重空格操作,
对字符串中的每个单词进行反转
对整个字符串进行反转
====
code
class Solution {
public:
void help_reverse(string &s,int start,int end){
while(start<end){///经典的反转字符串方法
swap(s[start++],s[end--]);
}
}
string removeDuplicateSpace(string s){
string res;
int b = ;
for(;b<(int)s.size();b++){
if(s[b]!= ' '){
break;
}
}///
int e = s.size() - ;
for(;e>=;e--){
if(s[e]!=' '){
break;
}
} bool is_space = false;
for(int i = b;i<=e;i++){
if(s[i] == ' '){
if(!is_space) is_space = true;
else continue;
}else{
is_space = false;
}
res.push_back(s[i]);
}
return res;
}
void reverseWords(string &s) {
if(s.empty()) return;
s = removeDuplicateSpace(s);
int start = ;
for(size_t i = ;i<s.size();i++){
if(s[i]!=' '){
start = i;
}else{
continue;
}
size_t j = i;
while(j<s.size() && s[j]!=' '){
j++;
}
j--;
help_reverse(s,start,j);
i = j++;
}
help_reverse(s,,(int)s.size()-);
}
};
151. Reverse Words in a String的更多相关文章
- 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 ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- [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】151 Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- (String)151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Java for 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 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- 151. Reverse Words in a String (String)
思路: 本题考查的目的并不是使用字符串的函数.方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转. 需要注意的是去除extra space,并且对全space字符串.以及最 ...
随机推荐
- 最短路径--SPFA 算法
适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...
- Java字段初始化的规律
class InitializeBookClass { { field=200; } public int field=100; public InitializeBookClass(int valu ...
- spark新能优化之多次使用RDD的持久化或checkPoint
如果程序中,对某一个RDD,基于它进行了多次transformation或者action操作.那么就非常有必要对其进行持久化操作,以避免对一个RDD反复进行计算. 此外,如果要保证在RDD的持久化数据 ...
- hihoCoder#1015 : KMP算法 (KMP模板)
代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<vector ...
- JSBinding + SharpKit / 安装SharpKit以及添加SharpKit工程
本文说明如何往 sln 中添加 SharpKit 工程,以及配置. SharpKit 工程用于将 C# 源代码编译成 JS 代码. QQ群 189738580 1. 安装SharpKit 到 sha ...
- JavaScript-闭包注意事项
闭包允许内层函数引用父函数中的变量,但是该变量是最终值
- C++中智能指针的设计和使用
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/7561235 智能指针(smart pointer)是存储指向动态分配(堆 ...
- C# 数组,ArrayList与List对象的区别
在C#中,当我们想要存储一组对象的时候,就会想到用数组,ArrayList,List这三个对象了.那么这三者到底有什么样的区别呢? 我们先来了解一下数组,因为数组在C#中是最早出现的. 数组 数组有很 ...
- 一个不错的vim配置
set nocp set backspace=indent,eol,start set number set autoindent set nocompatible set bs=indent,eol ...
- jQuery.fn.extend与jQuery.extend
jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑能跳,现在我们用jQuery.extend这个方法给这个类拓展一个能唱歌的技能.这 ...