转载请注明作者和出处: http://blog.csdn.net/c406495762
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
题目:反转字符串
思路:使用swap函数交换字符串位置。也可以使用reverse,更简单。
Language:cpp
class Solution {
public:
string reverseString(string s) {
int begin = 0, end = s.size() - 1;
while(begin < end){
swap(s[begin++], s[end--]);
}
return s;
}
};
Language:cpp
class Solution {
public:
string reverseString(string s) {
reverse(s.begin(),s.end());
return s;
}
};