Leetcode——344. Reverse String

2021-10-15 16:49:39 浏览数 (1)

思路很简单,从左到右依次替换就行:

代码语言:javascript复制
class Solution {
public:
    void reverseString(vector<char>& s) {
        int left = 0;
        int right = (int)s.size() - 1;
        if (right < 0) {
            return;
        }
        while (left < right) {
            swap(s[left  ], s[right--]);
        }
    }
};

0 人点赞