Sword Finger Offer Interview Question 58-II. Left Rotate String [Simple]

My solution:

1. Add the first n characters directly after the string

class Solution {
public:
    string reverseLeftWords(string s, int n) {
        for(int i=0;i<n;i++)
            s+=s[i];
        return string(s.begin()+n,s.end());
    }
};

2. Discover the excellent function of substr ()

s.substr (pos, n), returns a string containing a copy of n characters in s starting from pos

class Solution {
public:
    string reverseLeftWords(string s, int n) {
        return s.substr(n,s.length()-n+1)+s.substr(0,n);
    }
};

Published 65 original articles · Like1 · Visits 480

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105487962