【剑指offer】【字符串】58-II. 左旋转字符串

58. II左旋转字符串

题目链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/

class Solution {
public:
    //块交换问题: "abcdefg"; 先交换前2个:"bacdefg"; 再交换剩下的:"bagfedc"; 最后整体交换:"cdefgab"。
    string reverseLeftWords(string s, int n) {
        int len = s.size();
        for(int i = 0; i < n / 2; i++){
            swap(s[i], s[n -i - 1]);
        }
        for(int i = n; i < (len + n) / 2; i++){
            swap(s[i], s[len - (i - n) - 1]);
        }
        for(int i = 0; i < len / 2; i++)
            swap(s[i], s[len - i -1]);
        return s;
    }
};
原文地址:https://www.cnblogs.com/Trevo/p/13520762.html