leetcode

左旋转字符串

题目链接

// 整体反转 + 局部反转
// s = abcdefg,n=2
// a b c d e f g
// --> -------->
// b a g f e d c
// <-- <--------
// c d e f g a b
// --------> -->

class Solution {
public:
    std::string reverseLeftWords(std::string s, int n) {
        // 反转区间为前n的子串
        std::reverse(s.begin(), s.begin()+n);
        // 反转区间为n到末尾的子串
        std::reverse(s.begin()+n, s.end());
        // 反转整个字符串
        std::reverse(s.begin(), s.end());
        return s;
    }
};