#leetcode# 344 Reverse String

来源:互联网 发布:php content length 编辑:程序博客网 时间:2024/05/22 23:17

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) {        int start=0;        int end=s.size()-1;        int swap;        while(start<=end)        {            swap=s[start];            s[start]=s[end];            s[end]=swap;            start=start+1;            end=end-1;        }        return s;    }};






0 0