LeetCode 344. Reverse String 对撞指针

来源:互联网 发布:sqlserver代理无法启动 编辑:程序博客网 时间:2024/05/16 18:21

一、题目

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 left=0,right=s.length()-1;        while(left<right)        {            swap(s[left],s[right]);            left++;            right--;        }                return s;    }};


0 0
原创粉丝点击