您的位置:首页 > 其它

LeetCode(344) Reverse String

2016-08-15 22:14 357 查看

题目

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) {
if (s.empty())
return s;

int l = 0, r = s.size() - 1;
string ret = s;
while (l < r)
{
char t = ret[l];
ret[l] = ret[r];
ret[r] = t;

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