LeetCode:reverse words in a string---两种方法实现
2014-08-08 12:11
363 查看
方法一:
用到了递归思想,按空格把字符串从中间分成前后两部分,分别前后两部分互换,一直递归下去,即可达到目标。
如 s = “the sky is blue” 先把s分为s1 = “the sky” s2 = “ is blue” 交换可以变为 s = s2 + s1 = "is blue the sky"。若s2和s1也分别交换前后两部分 则s1‘ = “sky the” s2’ = “blue is”
s = s2‘ + s1’ = “blue is sky the”
代码如下:
class Solution {
public:
void reverseWords(string &s) {
if(s.size()!=0)
s = reverse(s);
}
string reverse(string s) {
eraseBlank(s);
if(s.find(" ") == s.npos || s == " " || s.size() == 0)
return s;
string::size_type position1 = 0;
string::size_type position2 = string::npos;
while(position1 <= position2)
{
position1 = s.find(" ",position1) + 1;
position2 = s.rfind(" ",position2) - 1;
}
string str1 = reverse(s.substr(position1 , s.size() - position1));
string str2 = reverse(s.substr(0 , position2 + 1));
string str3 = s.substr(position2 + 1,position1 - position2 -1);
eraseBlank(str3);
if(str3 == "")
return str1 + " " + str2;
str3 = " " + str3 + " ";
return str1 + str3 + str2;
}
void eraseBlank(string& s)
{
while(s.find(" ") == 0 || s.size() == 0)
{
if(s == ""){
return;
}
s.erase(0,1);
}
while(s.rfind(" ") == s.size() - 1)
s.erase(s.size() - 1,1);
return;
}
};
已AC; RunTime:64ms
方法二:用桟来倒置
从后往前遍历字符串,遇到非空格字符压入桟中,遇到空格字符,将桟中的字符弹出,形成一个单词,并追加在新字符串中,遍历完后即得到要求的字符串。其中需要注意空格的处理。
代码如下:
class Solution {
public:
void reverseWords(string &s) {
int index = s.size();
string strTemp = "";
stack<char> stackReverse;
while(index--)
{
if(s[index] != ' ')
stackReverse.push(s[index]);
else
{
if(stackReverse.size() == 0)
continue;
while(stackReverse.size() != 0)
{
strTemp += stackReverse.top();
stackReverse.pop();
}
if(index)
strTemp += " ";
}
}
while(stackReverse.size() != 0)
{
strTemp += stackReverse.top();
stackReverse.pop();
}
if(strTemp[strTemp.size()-1] == ' ')
strTemp = strTemp.substr(0,strTemp.length()-1);
s = strTemp;
}
};
已AC; RunTime:48ms
方法二中处理空格不是很巧妙,最后需要额外判断,求大家更快更简洁的方法!
用到了递归思想,按空格把字符串从中间分成前后两部分,分别前后两部分互换,一直递归下去,即可达到目标。
如 s = “the sky is blue” 先把s分为s1 = “the sky” s2 = “ is blue” 交换可以变为 s = s2 + s1 = "is blue the sky"。若s2和s1也分别交换前后两部分 则s1‘ = “sky the” s2’ = “blue is”
s = s2‘ + s1’ = “blue is sky the”
代码如下:
class Solution {
public:
void reverseWords(string &s) {
if(s.size()!=0)
s = reverse(s);
}
string reverse(string s) {
eraseBlank(s);
if(s.find(" ") == s.npos || s == " " || s.size() == 0)
return s;
string::size_type position1 = 0;
string::size_type position2 = string::npos;
while(position1 <= position2)
{
position1 = s.find(" ",position1) + 1;
position2 = s.rfind(" ",position2) - 1;
}
string str1 = reverse(s.substr(position1 , s.size() - position1));
string str2 = reverse(s.substr(0 , position2 + 1));
string str3 = s.substr(position2 + 1,position1 - position2 -1);
eraseBlank(str3);
if(str3 == "")
return str1 + " " + str2;
str3 = " " + str3 + " ";
return str1 + str3 + str2;
}
void eraseBlank(string& s)
{
while(s.find(" ") == 0 || s.size() == 0)
{
if(s == ""){
return;
}
s.erase(0,1);
}
while(s.rfind(" ") == s.size() - 1)
s.erase(s.size() - 1,1);
return;
}
};
已AC; RunTime:64ms
方法二:用桟来倒置
从后往前遍历字符串,遇到非空格字符压入桟中,遇到空格字符,将桟中的字符弹出,形成一个单词,并追加在新字符串中,遍历完后即得到要求的字符串。其中需要注意空格的处理。
代码如下:
class Solution {
public:
void reverseWords(string &s) {
int index = s.size();
string strTemp = "";
stack<char> stackReverse;
while(index--)
{
if(s[index] != ' ')
stackReverse.push(s[index]);
else
{
if(stackReverse.size() == 0)
continue;
while(stackReverse.size() != 0)
{
strTemp += stackReverse.top();
stackReverse.pop();
}
if(index)
strTemp += " ";
}
}
while(stackReverse.size() != 0)
{
strTemp += stackReverse.top();
stackReverse.pop();
}
if(strTemp[strTemp.size()-1] == ' ')
strTemp = strTemp.substr(0,strTemp.length()-1);
s = strTemp;
}
};
已AC; RunTime:48ms
方法二中处理空格不是很巧妙,最后需要额外判断,求大家更快更简洁的方法!
相关文章推荐
- leetcode 【 Reverse Words in a String 】python 实现
- LeetCode 之 Reverse Words in a String — C 实现
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- leetcode:151Reverse Words in a String java实现
- Reverse Words in a String[LeetCode]
- Leetcode 第151题 最新题解 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
- LeetCode151: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 JAVA Reverse Words in a String 难度系数2-3?最新题151
- Reverse Words in a String (LeetCode)
- 【LeetCode】Reverse Words in a String
- LeetCode(151)Reverse Words in a String