当前位置:网站首页>[leetcode] rotation series (array, matrix, linked list, function, string)
[leetcode] rotation series (array, matrix, linked list, function, string)
2022-06-24 18:47:00 【Xiaozhu Xiaozhu will never admit defeat】
【Leetcode】 The topics in the rotation series are summarized as follows :
List of articles
Rotation series problems
189. Rotation array
1. Title Description
leetcode link :189. Rotation array 

2. Thought analysis
according to k To rotate the array , The space complexity is required to be O(1), So you need to reverse the original array , So you can reverse the entire array first , Put the front k A reversal , Then put the back n-k A reversal .
It should be noted that :k Greater than nums.length The situation of , Need to use k Yes nums.length modulus .
3. Reference code
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
k = k % n;
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
int tmp = nums[start];
nums[start++] = nums[end];
nums[end--] = tmp;
}
}
}
Interview questions 01.07. Rotation matrix
1. Title Description
leetcode link : Interview questions 01.07. Rotation matrix

2. Thought analysis
3. Reference code
The finger of the sword Offer 24. Reverse a linked list
1. Title Description
leetcode link : The finger of the sword Offer 24. Reverse a linked list 
2. Thought analysis
Method 1 : Iterative method ( Double pointer )
Method 2 : recursive
Use recursion to traverse the linked list , When the tail node is crossed, the recursion is terminated , Modify the of each node during backtracking next Reference point .
3. Reference code
Method 1 : Iterative method ( Double pointer )
class Solution {
public ListNode reverseList(ListNode head) {
ListNode dumpy = null;
while (head != null) {
ListNode tmp = head.next;
head.next = dumpy;
dumpy = head;
head = tmp;
}
return dumpy;
}
}
Method 2 : recursive
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null){
return head;
}
ListNode node = reverseList(head.next);
head.next.next = head;
head.next = null;
return node;
}
}
61. Rotate the list
1. Title Description
leetcode link :61. Rotate the list

2. Thought analysis
3. Reference code
396. Rotation function
1. Title Description
leetcode link :396. Rotation function 
2. Thought analysis
3. Reference code
796. Rotate string
1. Title Description
leetcode link :796. Rotate string

2. Thought analysis
3. Reference code
边栏推荐
- 696. count binary substring
- Why are life science enterprises on the cloud in succession?
- Get max value of a bit column - get max value of a bit column
- How to create a linear model prediction interval in R and visualize it
- Solve the problem that the MapReduce program console does not have log information warn please initialize the log4j system properly
- JS deep understanding of functions
- 电源效率测试
- Microservice system design -- data model and system architecture design
- How to perform robust regression in R
- BOM(Browser Object Model)
猜你喜欢

JS deep understanding of functions

为什么生命科学企业都在陆续上云?

About pyqt5 to realize paging function (one window implements different interfaces)

Window object

JS string method

Freeswitch使用originate转dialplan

API管理之利剑 -- Eolink

Wechat applet development - Implementation of rotation chart

Vite+web3: referenceerror: process is not defined

JS pre parsing
随机推荐
2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
干货 | 新手经常忽略的嵌入式基础知识点,你都掌握了吗?
Selection (030) - what is the output of the following code?
微服务系统设计——数据模型与系统架构设计
Value passing and reference passing of value types and reference types in CSharp
Set up your own website (8)
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
Knowledge points in T-SQL
Differences between get and post request modes
Industry Cloud video editing software
电源效率测试
Solve the problem that the MapReduce program console does not have log information warn please initialize the log4j system properly
About pyqt5 to realize paging function (one window implements different interfaces)
Get max value of a bit column - get max value of a bit column
JS picture display and hiding cases
What is business intelligence (BI)?
Make track map
上位机与MES对接的几种方式
建立自己的网站(8)
How to perform robust regression in R