LeetCode 面试题58 - II. 左旋转字符串

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

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

示例 1:

输入: s = "abcdefg", k = 2
输出: "cdefgab"
示例 2:

输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"
 

限制:

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 }

 

posted @ 2020-03-03 22:45  wydxry  阅读(174)  评论(0编辑  收藏  举报
Live2D