[剑指 offer] JT43---Rotate the string to the left (all lost will return in another way!)

Sword Finger Offer Question 43

The topic is as follows

Insert picture description here

Idea and code

The cycle moves to the left, and it feels like I suddenly returned to the time of learning the principles of microcomputers.
Hey, I'm so nostalgic!
Just do it directly and you're done, too many ideas!
Two queues, or vector deletion and addition operations are also fine!
Here directly use the substr of c++ to complete it! Find the location directly, one size fits all, so cool!

class Solution {
    
    
public:
    string LeftRotateString(string str, int n) {
    
    
        int size=str.length();
        if(size==0) return "";
        else n%=size;
        string s1=str.substr(0,n);
        string s2=str.substr(n,size-n);
        return s2+s1;
    }
};

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42136832/article/details/115055488