Very clean c++


  • 0
    J
    class Solution {
    public:
       string reverseStr(string s, int k) {
          int len = s.size();
          bool revFlag = true;
          for (int i = 0; i < len; i += k) {
             rev (s, i, i + k - 1, revFlag);
             revFlag = !revFlag;
          }
          return s;
        }
    
       void rev(string &s, int start, int end, bool revFlag) {
           if (revFlag == false) {
               return;
           }
           end = min(end, static_cast<int>(s.size() - 1));
           while (start < end) {
               swap(s[start++], s[end--]);
           }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.