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.

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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& ...

  4. 【刷题-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: ...

  5. (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& ...

  6. 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 ...

  7. 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& ...

  8. 151. Reverse Words in a String翻转一句话中的单词

    [抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...

  9. 151. Reverse Words in a String (String)

    思路: 本题考查的目的并不是使用字符串的函数.方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转. 需要注意的是去除extra space,并且对全space字符串.以及最 ...

随机推荐

  1. java的加减乘除

    //编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果.//MengYao,2015,10,6 import javax.swing.JOptionPane;public class ...

  2. Codeforces Round #134 (Div. 2)

    A. Mountain Scenery 枚举山顶位置,满足\(r_{i-1} \lt r_i - 1 \gt r_{i+1}\). 范围要开\(2N\). B. Airport 优先队列维护最值. C ...

  3. JSON的转换(将JSON转换为字符串,将字符串转化为JSON)

    eval()和JSON.stringify()   1.eval用于从一个字符串中解析出json 对象,创建包含 JSON 语法的 JavaScript 字符串.例如 var str = '{ &qu ...

  4. php开发memcached

    一.memcached 简介 memcached是高性能的分布式内存缓存服务器.一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度.提高可扩展 性.它可以应对任意 ...

  5. javascript error

    IE 6下expected identifier,string or number(缺少标示符.字符串或数字)其实就是多了一个逗号,之前没发现

  6. 013. asp.net统计网站访问人数

    Global.asax中的代码: <%@ Application Language="C#" %> <script runat="server" ...

  7. 004. 线程间操作无效: 从不是创建控件“textBox1”的线程访问它

    最简单的方法(不推荐): 在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false; 为什么不推荐上面的方法: 为避免空间造成死锁, .net f ...

  8. 让Js顺序执行且回调之

    <script src="aaa"></script> <script type="aaasdf" id="asdf&q ...

  9. OpenJudge计算概论-称体重【枚举法、信息数字化】

    /*====================================================================== 称体重 总时间限制: 1000ms 内存限制: 655 ...

  10. hadoop(四): 本地 hbase 集群配置 Azure Blob Storage

    基于 HDP2.4安装(五):集群及组件安装 创建的hadoop集群,修改默认配置,将hbase 存储配置为 Azure Blob Storage 目录: 简述 配置 验证 FAQ 简述: hadoo ...