LeetCode : Reverse Words in a String
2014-05-21 17:48
211 查看
Given an input string, reverse the string word by word.
For example,
Given s = "
return "
click to show clarification.
For example,
Given s = "
the sky is blue",
return "
blue is sky the".
click to show clarification.
void reverseWords(string &s) { int length=s.size(); if(length==0) return ; int pre=0; int post=length-1; while(pre<post){ swap(s[pre],s[post]); ++pre; --post; } int start=0; int end=0; while(end<length){ while(end<length&&s[end]!=' '){ end++; } pre=start; post=end-1; while(pre<post){ //swap(s[pre],s[post]); char ch=s[pre]; s[pre]=s[post]; s[post]=ch; ++pre; --post; } while(end<length&&s[end]==' '){ end++; } start=end; } int len=s.size(); int low=0; int fast=0; while(fast<len){ if(s[fast]!=' '||((fast+1<len)&&s[fast]==' '&&s[fast+1]!=' '&&low!=0)){ low++; } fast++; s[low]=s[fast]; } s.resize(low,'\0');
相关文章推荐
- Leetcode 第151题 最新题解 Reverse Words in a String
- leetcode JAVA Reverse Words in a String 难度系数2-3?最新题151
- LeetCode(151)Reverse Words in a String
- Leetcode: Reverse Words in a String
- LeetCode Reverse Words in a String
- 【LeetCode with Python】 Reverse Words in a String
- [LeetCode]Reverse Words in a String
- leetcode Reverse Words in a String
- [leetcode]Reverse Words in a String
- Leetcode_Reverse-Words-in-a-String
- LeetCode | Reverse Words in a String
- Reverse Words in a String (LeetCode)
- [Leetcode Solution] Reverse Words in a String
- LeetCode151:Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- [leetCode]Reverse Words in a String
- leetcode - Reverse Words in a String
- [LeetCode] Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- 【LeetCode】Reverse Words in a String