leetCode(34):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 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.

字符串反转的问题,有很多种方法。但本题要求删除多余的空格,所以比较特别,最直接的方法就是把所有单词取出来放到栈当中,再弹出。但题目要求不能用多余的辅助空间,所以只能在翻的时候去掉多余的空格。

class Solution {
public:
    void reverseWords(string &s) {
        reverse(s.begin(),s.end());
//首先去除头尾的空格
    	while(*(s.begin())==' ')
    	{
    		s.erase(s.begin());
    	}

    	while(*(s.end()-1)==' ')
    	{
    		s.erase(s.end()-1);
    	}
    	string::iterator iter1=s.begin();
    	string::iterator iter2=s.begin();

    	for(;iter2!=s.end();++iter2)
    	{
    		if(*iter2==' ' && *iter1!=' ')
    		{//反转每一个单词
    			reverse(iter1,iter2);
    			iter1=iter2+1;
    		}
    		else if(*iter2==' ' && *iter1==' ')
    		{//去除中间的空格
    			iter1--;
    			s.erase(iter2);
    			iter2=iter1;
    			iter1++;
    		}
    	}
    	reverse(iter1,s.end());//最后一个单词
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 23:35:23

leetCode(34):Reverse Words in a String的相关文章

【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

LeetCode OJ1: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". 解题思路: 先利用split()方法将句子按空格分为几个单词的列表, 然后reverse()函数使列表逆序, 最后join函数以空格为间隔拼成字符串返回结果. Python代码: class Solution:  

LeetCode OJ - 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.先对字符串进行一次总的翻转 2.以空格为单位,划分单词,然后对每个单词再进行一次翻转 代码: class Solution { public: void swap(char& a, char

[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

题目 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.

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

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 - [1]Reverse Words in a String

Question: 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 con