Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
思路:两个指针,头尾交换,算法复杂度:O(n/2).
class Solution {
public:
string reverseString(string s) {
int len= s.length();
if(len<=0)
{
return s;
}
for(int i=0; i<len/2; ++i){
char tmp = s[i];
s[i]=s[len-i-1];
s[len-i-1]=tmp;
}
return s;
}
};