[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”,

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.

思路

两次反转法。这里注意单词之间的多个空格只留一个空格以及去掉前导0和后导0。

代码

/*---------------------------------------
*   日期:2015-05-05
*   作者:SJF0115
*   题目: 151.Reverse Words in a String
*   网址:https://leetcode.com/problems/reverse-words-in-a-string/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    void reverseWords(string &s) {
        int size = s.size();
        if(size <= 0){
            return;
        }//if
        // 前导0
        int index = 0;
        while(s[index] == ‘ ‘){
            ++index;
        }//while
        int left = index;
        // 后导0
        index = size - 1;
        while(s[index] == ‘ ‘){
            --index;
        }//while
        int right = index;
        // 连续空格只保留一个
        for(int i = left;i <= right;){
            if(i > left && s[i-1] == ‘ ‘ && s[i] == ‘ ‘){
                s.erase(i-1,1);
                --right;
            }//if
            else{
                ++i;
            }
        }//for
        // 翻转
        int start = left,end = left;
        for(int i = left;i <= right+1;++i){
            if(i == right+1 || s[i] == ‘ ‘){
                Reverse(s,start,end-1);
                ++end;
                start = end;
            }//if
            else{
                ++end;
            }//else
        }//for
        // 整体翻转
        Reverse(s,left,right);
        s = s.substr(left,right - left + 1);
    }
private:
    void Reverse(string &str,int start,int end){
        for(int i = start,j = end;i < j;++i,--j){
            swap(str[i],str[j]);
        }//for
    }
};

int main() {
    Solution solution;
    //string str("   abc    cd     ef ");
    string str("  a   vvv  ");
    solution.reverseWords(str);
    cout<<str<<endl;
}

运行时间

时间: 2024-10-12 11:02:10

[LeetCode]151.Reverse Words in a String的相关文章

leetCode 151. Reverse Words in a String 字符串反转 | Medium

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". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候

[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",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

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",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. 倒序字符串. 注意使用BufferString,因为如果使用Stri

Leetcode 151. Reverse Words in a String 解题报告

[Problem] 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. [Idea] 从左往右遍历一次,每当遇到空格暂停

LeetCode 151 reverse word 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. 这题遇到的困难主要是细节地方处理的不好:(1)空格的处理,(2

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", return "blue is sky the". 解题思路: 本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下: public String reverseWords(String s) { if (s == null || s.length()

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

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". What constitutes a word?A sequence of non-space characters constitutes a word. Cou