当前位置:网站首页>Question swiping Madness - leetcode's sword finger offer58 - ii Detailed explanation of left rotation string
Question swiping Madness - leetcode's sword finger offer58 - ii Detailed explanation of left rotation string
2022-07-29 05:35:00 【Gancheng なつき】
꧁ Hello, everybody ! It is a great honor to have your visit , Let's have a long way to go in programming !꧂
* Blog column :【LeetCode】*
Introduction to this article :LeetCode Sword finger offer58 - II. Left rotation string Detailed explanation !
Get to know the author : Aspire to be a great programmer , Xiaobai, who is currently a sophomore in College .
Inspirational terms : The tediousness of programming , Let's study together and become interesting !

List of articles
Method Two ( Three step reverse order method )
subject
The left rotation operation of string is to transfer several characters in front of string to the end of string . Please define a function to implement the left rotation operation of string . such as , Input string "abcdefg" And number 2, This function will return the result of rotating two bits to the left "cdefgab".
link :https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof source : Power button (LeetCode)
Example 1:
Input : s = "abcdefg", k = 2
Output : "cdefgab"Example 2:
Input : s = "lrloseumgh", k = 6
Output : "umghlrlose"Method One ( Circulation )
Their thinking
For a string, it's just left rotation , It is certain that this string can be changed , So it's not a constant string , We use arrays to represent , Secondly, we can take out every character before the string every time , Then take each character of the original string forward , Finally, put the characters taken out at the end of the string , For a cycle , How many characters to rotate is how many times to cycle !
Let's draw a picture to explain :

The idea should be clear ! Now let's implement the code :
Method ( One ) — Code
#include<stdio.h>
#include<assert.h>
#include<string.h>
void left_revolve(char arr[], int k)// Implementation function
{
assert(arr);// Assert , Prevent null pointer
int len = strlen(arr);// Total string length
// When k>len Time is the problem of reincarnation
k %= len;
int i = 0;
for (i = 0; i < k; i++)
{
char tmp = arr[0];// Take out the first character to store
int j = 0;
for (j = 0; j < len - 1; j++)
{
arr[j] = arr[j + 1];// Replace character forward
}
arr[len - 1] = tmp;
}
}
int main()
{
char arr[] = { "abcdefg" };
int k = 0;
scanf("%d", &k);// How many characters to rotate
// Realize its function with function
left_revolve(arr, k);
printf("%s\n", arr);// Print the results
return 0;
}Let's look at the results :

Method Two ( Three step reverse order method )
Problem solving Ideas
One : We can first divide the string into two parts ,1. Is the string to rotate 2. The rest of the string .
Two : Then put them in reverse order .
3、 ... and : Finally, reverse the whole order
Let's draw a picture , The result of the person who gets the map :

Method ( Two ) — Code
#include<stdio.h>
#include<assert.h>
#include<string.h>
void reverse(char* left, char* right)// Reverse string function , It's a pointer
{
assert(left && right);
while (left < right)
{
char tmp = *left;// Back and forth
*left = *right;
*right = tmp;
left++;
right--;
}
}
void left_revolve(char arr[], int k)
{
assert(arr);
// Reverse string
int len = strlen(arr);
k %= len;
reverse(arr, arr + k - 1);// String to rotate left in reverse order
reverse(arr + k, arr + len - 1);// The remaining strings in reverse order
reverse(arr, arr + len - 1);// Reverse the entire string
}
int main()
{
char arr[] = { "abcdefg" };
int k = 0;
scanf("%d", &k);
left_revolve(arr, k);
printf("%s", arr);
return 0;
}
Look at the results :

Conclusion
Ok! At this point, the blog is finished , What's wrong or bad , Please help readers correct ( Just leave a comment below ), Thank you all !
边栏推荐
猜你喜欢

一维数组练习

365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t

实现简单的数据库查询(不完整)

Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"

Selenium实战案例之爬取js加密数据

C语言 一级指针

三次握手四次挥手针对面试总结

365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value

C语言 一维数组

虚拟增强与现实第二篇 (我是一只火鸟)
随机推荐
redis的基本使用
Day 5
阿里云联合鼎捷软件发布云上数字工厂解决方案,实现云MES系统本地化部署
第三课threejs全景预览房间案例
Clickhouse learning (x) monitoring operation indicators
rem与px与em异同点
【C语言系列】—文件操作详解(上)
Day 3
Clickhouse learning (IX) Clickhouse integrating MySQL
Allocate memory: malloc() and free()
【C语言系列】—深度解剖数据在内存中的存储(二)-浮点型
冒泡排序 C语言
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
一维数组练习
无重复字符的最长字串
浅谈Servlet
省市区三级联动(简单又完美)
167. Sum of two numbers II - enter an ordered array
uniapp之常用提示弹框
Time complexity and space complexity