当前位置:网站首页>刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
2022-07-29 05:09:00 【甘城なつき】
꧁ 各位大佬们好!很荣幸能够得到您的访问,让我们一起在编程道路上任重道远!꧂
* 博客专栏:【LeetCode】*
本篇内容简介:LeetCode之剑指offer58 - II. 左旋转字符串 详解!
了解作者:励志成为一名编程大牛的学子,目前正在升大二的编程小白。
励志术语:编程道路的乏味,让我们一起学习变得有趣!

文章目录
题目
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
链接:https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof 来源:力扣(LeetCode)
示例 1:
输入: s = "abcdefg", k = 2
输出: "cdefgab"示例 2:
输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"方法 一 (循环法)
解题思路
对于一个字符串左旋转而已,肯定这个字符串是可以改变的,所以不是常量字符串,我们用数组来表示,其次我们可以先每次拿出字符串之前的每一位字符,再将原来的字符串的每一个字符往前拿,最后将拿出的字符放到字符串的最后面,为一个循环,旋转多少个字符就是要循环多少次!
我们来画图解释一下:

思路应该清晰明了了吧!现在我们把代码实现一下:
方法(一) — 代码
#include<stdio.h>
#include<assert.h>
#include<string.h>
void left_revolve(char arr[], int k)//实现函数
{
assert(arr);//断言一下,防止是空指针
int len = strlen(arr);//字符串总长度
//当k>len时就是轮回问题了
k %= len;
int i = 0;
for (i = 0; i < k; i++)
{
char tmp = arr[0];//拿出首字符存储
int j = 0;
for (j = 0; j < len - 1; j++)
{
arr[j] = arr[j + 1];//往前替换字符
}
arr[len - 1] = tmp;
}
}
int main()
{
char arr[] = { "abcdefg" };
int k = 0;
scanf("%d", &k);//要旋转多少个字符
//用函数实现其功能
left_revolve(arr, k);
printf("%s\n", arr);//打印结果
return 0;
}我们来看运行结果:

方法 二 (三步逆序法)
解题 思路
一 :我们可以先吧字符串分开成两个部分,1.是要旋转的字符串 2.剩下的字符串 。
二:再将它们分别逆序。
三:最后将整个一起逆序
我们来画个图,得图者得结果:

方法 (二) — 代码
#include<stdio.h>
#include<assert.h>
#include<string.h>
void reverse(char* left, char* right)//逆序字符串函数,传递的是指针
{
assert(left && right);
while (left < right)
{
char tmp = *left;//前后交换
*left = *right;
*right = tmp;
left++;
right--;
}
}
void left_revolve(char arr[], int k)
{
assert(arr);
//逆序字符串
int len = strlen(arr);
k %= len;
reverse(arr, arr + k - 1);//逆序要左旋转的字符串
reverse(arr + k, arr + len - 1);//逆序剩下的字符串
reverse(arr, arr + len - 1);//逆序整个字符串
}
int main()
{
char arr[] = { "abcdefg" };
int k = 0;
scanf("%d", &k);
left_revolve(arr, k);
printf("%s", arr);
return 0;
}
看运行结果:

结束语
Ok!至此该篇博客就算完成了,有什么错误或者不好的地方,请读者帮忙纠正(在下方评论留言即可),感谢各位读者!
边栏推荐
- CryEngine技术
- Adb常用命令列表
- With frequent data leakage and deletion events, how should enterprises build a security defense line?
- The road to success in R & D efficiency of 1000 person Internet companies
- Handwritten student management system
- 递归的基本原理
- 2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
- ANSI C类型限定符
- Do you remember the process analysis and function implementation of post notification?
- Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
猜你喜欢

Live broadcast Preview: integration of JD cloud Devops and jfrog product library

直播预告|如何通过“智能边缘安全”提升企业免疫力?

Unity3d - the object is too far away to see

6.3 references

Architecture analysis of three-tier project and parameter name injection of construction method

7.3-function-templates

Webrtc audio anti weak network technology (Part 2)

QML control: combobox

阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
![[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start](/img/6e/6b4deeedbfd9d6baa651019f3dabfa.jpg)
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
随机推荐
CryEngine技术
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
QT学习:QDropEvent拖拽事件
Teardown 解除时间限制的方法
MFC集成qt验证及问题处理
Cmu15-213 malloc lab experiment record
167. 两数之和 II - 输入有序数组
D3d Shader Instruction
2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
7.3-function-templates
Getting started with arfoundation tutorial 10- plane detection and placement
6.2 function-parameters
365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
Arfoundation starts from zero 9-ar anchor
How rimworld uploads creative workshops through steamcmd
CryEngine3 调试Shader方法
321,京东言犀×NLPCC 2022挑战赛开赛!
200 多家 ISV 入驻!阿里云计算巢发布一周年
C语言宏#define命令练习
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭