LeetCode face questions 58 - II string left rotation.

Topic links: https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/

Left rotational operation in front of the string is the string of several characters to the end of the string transfer. Define a function to achieve a left rotation operation function string. For example, the input string "abcdefg" and the number 2, this function returns two results obtained left rotation "cdefgab".

Example 1:

Input: s = "abcdefg", k = 2
Output: "cdefgab"
Example 2:

Input: s = "lrloseumgh", k = 6
Output: "umghlrlose"
 

limit:

1 <= k < s.length <= 10000

 1 char* reverseLeftWords(char* s, int n){
 2     int len=strlen(s);
 3     n%=len;
 4     if(n==0) return s;
 5     char *str=(char *)malloc(sizeof(char)*n);
 6     int i,j;
 7     for(i=0;i<n;i++){
 8         str[i]=s[i];
 9     }
10     for(i=n;i<len;i++){
11         s[i-n]=s[i];
12     }
13     for(i=0;i<n;i++){
14         s[i+len-n]=str[i];
15     }
16     return s;
17 }

 

Guess you like

Origin www.cnblogs.com/shixinzei/p/12405313.html