当前位置:网站首页>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 !
边栏推荐
- Clickhouse learning (x) monitoring operation indicators
- 用sql-client.sh生成的job在cancle过后 如何实现断点续传?
- paddle.fluild常量计算报错‘NoneType‘ object has no attribute ‘get_fetch_list‘
- With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
- 适创科技以云仿真平台,支持“中国智造”升级
- Cryengine5 shader debugging
- 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
猜你喜欢
随机推荐
Topological ordering of a graph of water
Selenium实战案例之爬取js加密数据
Cryengine Technology
Basic principle of recursion
D3d Shader Instruction
paddle. Fluid constant calculation error 'nonetype' object has no attribute 'get_ fetch_ list‘
365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
Allocate memory: malloc() and free()
Detailed explanation of GPIO input and output
重定向和文件
ClickHouse学习(六)语法优化
ClickHouse学习(四)SQL操作
Realize simple database query (incomplete)
个人学习笔记
Application of Huffman tree and Huffman coding in file compression
小程序中的DOM对象元素块动态排序
JS deep copy - Notes
Thrift安装手册
C language one-dimensional array
uniapp之常用提示弹框









