Leetcode 344. Reverse String

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

《Leetcode 344. Reverse String》 Reverse String

2. Solution

class Solution {
public:
    string reverseString(string s) {
        int i = 0;
        int j = s.length() - 1;
        while(i < j) {
            swap(s[i++], s[j--]);
        }
        return s;
    }

private:
    void swap(char& a, char& b) {
        char temp = a;
        a = b;
        b = temp;
    }
};

Reference

  1. https://leetcode.com/problems/reverse-string/description/
    原文作者:SnailTyan
    原文地址: https://www.jianshu.com/p/a12b9e00499c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞