您的位置:首页 > 其它

344. Reverse String

2016-05-23 15:06 288 查看
Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

Subscribe to see which companies asked this question

临时对象
class Solution {
public:
string reverseString(string s) {
int length=s.size();
string str_tmp(length,0);
int j=0;
for(int i=s.size()-1;i>=0;i--)
{
str_tmp[j++]=s[i];
}

return str_tmp;
}
};

原址交换

class Solution {
public:
string reverseString(string s) {
int j=s.size()-1;

for(int i=0;i<j;i++,j--)
{
swap(s[i],s[j]);
}

return s;
}
};
牛逼的C++

class Solution {
public:
string reverseString(string s) {
return string(s.rbegin(), s.rend());
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Reverse String