Eagle233-Blog

[Algorithms] LeetCode 541. 反转字符串 II


Categories Algorithms String
Tags

106 Words   |   1 Minutes

来源:代码随想录

LeetCode 541. 反转字符串 II

单指针

玩几天脑子进水了,这题都不会做了。后继比较难的可以直接用reverse,没必要再自己写了。

class Solution {
public:
    string reverseStr(string s, int k) {
        for (int i = 0; i < s.size(); i += 2 * k) {
            if (i + k < s.size()) {
                reverse(s.begin() + i, s.begin() + i + k);
            } else {
                reverse(s.begin() + i, s.end());
            }
        }

        return s;
    }
};


Page views: Loading...  ·  Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号
Search