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

来源:互联网 发布:深圳网络推广qebang 编辑:程序博客网 时间:2024/06/07 10:12

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;    }};


0 0