ResverString
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
1、STL
class Solution {
public:
string reverseString(string s) {
reverse(s.begin(),s.end());
return s;
}
};
2、swap in-place
class Solution {
public:
string reverseString(string s) {
auto i=0;
auto j=s.size();
if(j==0)
return s;
else
j--;
while(i<j){
swap(s[i++],s[j--]);
}
return s;
}
};