1. 题目分析
题目描述:Write a function that takes a string as input and returns the string reversed. Example: Given s = “hello”, return “olleh”. 题目含义:反转字符串 题目分析:从后之前取原串的元素并赋给新串即可。
2. 题目解答
class Solution {
public:
string reverseString(
string s) {
if (s.size() ==
0) {
return "";
}
string newstr;
for (
int i = s.size()-
1; i >=
0; --i) {
newstr += s[i];
}
return newstr;
}
};
3. 心得体会
在对串进行遍历时,注意 i 的取值范围。