当前位置:网站首页>[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
边栏推荐
- DOM (document object model)
- Use ado Net call stored procedure
- subject may not be empty [subject-empty]
- Vite+web3:报错出现ReferenceError: process is not defined
- Introduction and tutorial of SAS planet software
- LabView之MQTT协议使用
- API管理之利剑 -- Eolink
- JS deep understanding of scope
- Architecture decryption from distributed to microservice: several common microservice architecture schemes
- SAP license: ERP for supply chain management and Implementation
猜你喜欢

Three layer switching experiment

Make track map

Huitongda officially landed at the Hong Kong Stock Exchange: the gross profit margin continued to decline, and the book value of several shareholders still suffered losses

History object

Recommend a distributed JVM monitoring tool, which is very practical!

Vite+web3:报错出现ReferenceError: process is not defined

如何在 R 中使用 Fisher 的最小显着性差异 (LSD)

Why are life science enterprises on the cloud in succession?

FROM_ GLC introduction and data download tutorial

Creating a new MySQL user in Amazon RDS environment - creating a new MySQL user in Amazon RDS environment
随机推荐
R中的指数回归
Building MVC system based on three-tier structure
JS picture switching case
Introduction to alos satellite
1: Mosaic of 100W basic geographic information data
PLC功能块系列之多段曲线控温FB(SCL程序)
SAP license: ERP for supply chain management and Implementation
In depth learning of movement methods
JS deep understanding of functions
JS event details
微服务系统设计——数据模型与系统架构设计
Why are life science enterprises on the cloud in succession?
【leetcode】838. Push domino (Analog)
Sudoku (easy to understand)
What is decision intelligence?
Mcu-08 interrupt system and external interrupt application
Bigdecimalavoiddoubleconstructorrule: do not directly use the double variable as a parameter to construct BigDecimal
论文解读(SR-GNN)《Shift-Robust GNNs: Overcoming the Limitations of Localized Graph Training Data》
Five advantages and disadvantages of Bi
110. balanced binary tree