leetCode(34):Reverse Words in a String

原创 2015年07月08日 09:23:09

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());//最后一个单词
    }
};


LeetCode 186. Reverse Words in a String II(反转单词)

原题网址:https://leetcode.com/problems/reverse-words-in-a-string-ii/ Given an input string, reverse th...
  • jmspan
  • jmspan
  • 2016年05月25日 01:36
  • 804

[leetcode] 151. Reverse Words in a String 解题报告

题目链接:https://leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reverse the s...
  • qq508618087
  • qq508618087
  • 2016年01月18日 14:40
  • 1558

【LeetCode】Reverse Words in a String III 解题报告

【LeetCode】Reverse Words in a String III 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/rev...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017年04月12日 11:48
  • 1113

LeetCode(151)Reverse Words in a String

题目如下: Given an input string, reverse the string word by word. For example, Given s = "the sky is blu...
  • feliciafay
  • feliciafay
  • 2014年03月10日 02:47
  • 3772

[LeetCode]557. Reverse Words in a String III(反转字符串 III)

557. Reverse Words in a String IIIGiven a string, you need to reverse the order of characters in eac...
  • whl_program
  • whl_program
  • 2017年04月10日 00:46
  • 956

Leetcode 557. Reverse Words in a String III

题目:Given a string, you need to reverse the order of characters in each word within a sentence while ...
  • jessir
  • jessir
  • 2017年04月10日 19:48
  • 1006

(LeetCode) Reverse Words in a String

problem :Reverse Words in a String Given an input string, reverse the string word by ...
  • zhangyuehuan
  • zhangyuehuan
  • 2014年04月13日 09:26
  • 8220

【LeetCode刷题Java版】Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", r...
  • bruce_6
  • bruce_6
  • 2014年10月13日 14:21
  • 2116

【Leetcode】557. Reverse Words in a String III

思路: 用空格分割字符串,对每个子串,逆序,添加到结果数组。 public class Solution { public String reverseWords(String s) { ...
  • zhouziyu2011
  • zhouziyu2011
  • 2017年04月09日 13:18
  • 1791

LeetCode 557 Reverse Words in a String III(逆转字符串)

Given a string, you need to reverse the order of characters in each word within a sentence while sti...
  • qq_26658823
  • qq_26658823
  • 2017年04月14日 19:45
  • 449
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetCode(34):Reverse Words in a String
举报原因:
原因补充:

(最多只允许输入30个字)