题目要求
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
解题思路
逆转一个字符串,注意空格就行。
复杂度分析
时间复杂度O(n)。
代码如下:
class Solution {
public:
string reverseString(string s) {
string res="";
int n=s.length();
for (int i=n-1; i>=0; i--) {
res.push_back(s[i]);
}
return res;
}
};