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.
判断size=0 直接return
没啥说的,直接swap(s[i]. s[size-1-i]),
代码:
class Solution {
public:
string reverseString(string s) {
if(s.size() == 0)
return s;
int size = s.size();
for(int i = 0 ; i < size / 2; i ++)
swap(s[i],s[size-1-i]);
return s;
}
};