原題鏈接:Reverse String
題目內容:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
Python
class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
a = [letter for letter in s]
a.reverse()
return "".join(a)
C++
class Solution {
public:
string reverseString(string s) {
for(int i=0,j=s.size()-1;i<j;i++,j--){
char c=s[i];
s[i]=s[j];
s[j]=c;
}
return s;
}
};