您的位置:首页 > 编程语言 > C语言/C++

LeetCode 344:Reverse String (c++)

2017-06-21 09:02 316 查看
一:题目

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

二:题目分析

从字符串的结束为止,开始遍历,将其赋值给一个新的字符串

三:代码实现

class Solution {
public:
string reverseString(string s) {
//反转字符串,从结束位置开始遍历字符串,并将其赋值给另一个字符串
string ReverseString;
int i;

for(i=s.length()-1;i>=0;i--)
ReverseString+=s[i];

return ReverseString;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息