您的位置:首页 > 其它

LeetCode----344. Reverse String 字符串反转

2016-08-04 16:51 253 查看
344. Reverse String

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

Example:

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

设两个指针,分别指向字符串的首和尾,同时向中间移动,移动次数为字符串长度的1/2.

class Solution {
public:
string reverseString(string s) {
if(s.size()==0||s.size()==1){
return s;
}
char *f,*l;
f=&s[0];
l=&s[s.size()-1];
for(int i=0;i<s.size()/2;i++){
char tmp=*f;
*f++=*l;
*l--=tmp;
}
return s;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: