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

[LeetCode]344. Reverse String(反转字符串)

2017-03-01 00:52 465 查看

344. Reverse String

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

题目大意:反向输出字符串

Example:

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


代码如下

C++

class Solution {
public:
string reverseString(string s) {
int a = s.size()-1;
string b = "";
for(int i=a;i>=0;i--)
b += s[i];
return b;
}
};


注意:设置a是为了不用每次循环都执行s.size()-1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息