当前位置:网站首页>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 !
边栏推荐
- Basic use of redis
- ANSI C type qualifier
- 适创科技以云仿真平台,支持“中国智造”升级
- C language n queen problem
- Clickhouse learning (x) monitoring operation indicators
- ClickHouse学习(七)表查询优化
- Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
- Time complexity and space complexity
- 365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
- 【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
猜你喜欢
Do students in the science class really understand the future career planning?
C语言 一维数组
Alibaba cloud architect details nine trends in the game industry
C language one-dimensional array
Application of Huffman tree and Huffman coding in file compression
ClickHouse学习(十一)clickhouseAPI操作
PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
Redirection and files
Database operation day 6
随机推荐
实现简单的数据库查询(不完整)
【C语言系列】— 打印100~200之间的素数
ClickHouse学习(五)集群操作
科班同学真的了解未来的职业规划吗?
分配内存:malloc()和free()
Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system
href与src的区别
指针
[C language series] - print prime numbers between 100 and 200
使用微信小程序扫码登录系统PC端web的功能
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
ClickHouse学习(十一)clickhouseAPI操作
冒泡排序 C语言
365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
三次握手四次挥手针对面试总结
Cryengine3 debugging shader method
Occt learning 002 - environment construction
抽象类与接口
redis的基本使用