当前位置:网站首页>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 !
边栏推荐
- rem与px与em异同点
- C language first level pointer
- Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
- 基础爬虫实战案例之获取游戏商品数据
- One dimensional array exercise
- Day 3
- eggjs 创建应用知识点
- PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍
- 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
- Using POI TL to insert multiple pictures and the same data of multiple rows of cells into the table cells of word template at one time, it is a functional component for automatic merging
猜你喜欢

无重复字符的最长字串

365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election

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

Occt learning 002 - environment construction

VIM editor use

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

阿里云架构师细说游戏行业九大趋势

第三课threejs全景预览房间案例

Detailed explanation of serial port communication

省市区三级联动(简单又完美)
随机推荐
Abstract classes and interfaces
Detailed explanation of exit interrupt
One dimensional array exercise
抢先预约 | 阿里云无影云应用线上发布会预约开启
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
Day 1
ClickHouse学习(十一)clickhouseAPI操作
365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
C语言 一级指针
浅谈范式
携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
Introduction to array learning simple question sum of two numbers
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
终端shell常用命令
冒泡排序 C语言
Integer overflow and printing
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
With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
Flask 报错 RuntimeError: The session is unavailable because no secret key was set.