当前位置:网站首页>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 !
边栏推荐
猜你喜欢
![[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises](/img/6f/f7c5d605ea9b7b9e7c49ac716492ef.jpg)
[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises

B - 识别浮点常量问题

Longest string without duplicate characters

携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”

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

Alibaba cloud architect details nine trends in the game industry

ClickHouse学习(二)ClickHouse单机安装

阿里云联合鼎捷软件发布云上数字工厂解决方案,实现云MES系统本地化部署

ClickHouse学习(七)表查询优化

B - identify floating point constant problems
随机推荐
C language file operation
ANSI C类型限定符
[C language series] - realize the exchange of two numbers without creating the third variable
Day 5
Introduction to array learning simple question sum of two numbers
365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
Best practices for elastic computing in the game industry
Provincial and urban three-level linkage (simple and perfect)
时间复杂度和空间复杂度
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
浅谈范式
冒泡排序 C语言
【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
阿里云架构师细说游戏行业九大趋势
End of document
C language n queen problem
shell基本操作(上)
AR虚拟增强与现实
无重复字符的最长字串
题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)