当前位置:网站首页>【Leetcode】旋转系列(数组、矩阵、链表、函数、字符串)
【Leetcode】旋转系列(数组、矩阵、链表、函数、字符串)
2022-06-24 18:33:00 【小朱小朱绝不服输】
【Leetcode】旋转系列题目总结如下:
旋转系列问题
189. 轮转数组
1.题目描述
leetcode链接:189. 轮转数组

2.思路分析
根据k来轮转数组,要求空间复杂度为O(1),因此需要在原数组上进行反转,因此可以先反转整个数组,再把前k个反转,再把后n-k个反转。
需要注意的是:k大于nums.length的情况,需要用k对nums.length取模。
3.参考代码
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;
}
}
}
面试题 01.07. 旋转矩阵
1.题目描述
leetcode链接:面试题 01.07. 旋转矩阵

2.思路分析
3.参考代码
剑指 Offer 24. 反转链表
1.题目描述
leetcode链接:剑指 Offer 24. 反转链表
2.思路分析
方法一:迭代法(双指针)
方法二:递归
使用递归法遍历链表,当越过尾节点后终止递归,在回溯时修改各节点的 next 引用指向。
3.参考代码
方法一:迭代法(双指针)
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;
}
}
方法二:递归
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. 旋转链表
1.题目描述
leetcode链接:61. 旋转链表

2.思路分析
3.参考代码
396. 旋转函数
1.题目描述
leetcode链接:396. 旋转函数
2.思路分析
3.参考代码
796. 旋转字符串
1.题目描述
leetcode链接:796. 旋转字符串

2.思路分析
3.参考代码
边栏推荐
- Industry Cloud video editing software
- 717.1-bit and 2-bit characters [sliding window]
- Application service access configuration parameters
- Selection (033) - what is the output of the following code?
- C self learning function
- Restful design method
- Microservice system design - sub service project construction
- 696. count binary substring
- BOM(Browser Object Model)
- Mcu-08 interrupt system and external interrupt application
猜你喜欢

DOM (document object model)

Microservice system design -- data model and system architecture design

Ten excellent business process automation tools for small businesses

Conception de systèmes de micro - services - construction de sous - services
What if the database table structure changes? Smartbi products support one click synchronization

Flutter dart regular regexp special characters $, () (IV)

Leetcode weekly buckle 281

JS string method
[golang] leetcode intermediate - jumping game & different paths

【leetcode】838. Push domino (Analog)
随机推荐
Leetcode skimming questions - the 72nd biweekly match and 281 weekly match
What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
Why are more and more people studying for doctors? Isn't it more and more difficult to graduate a doctor?
Five advantages and disadvantages of Bi
Microservice system design -- interface document management design
An analysis of the comments on the TV series Douban by procedural apes
Top ten popular codeless testing tools
Monotone stack template
Digital trend analysis of B2B e-commerce market mode and trading capacity in electronic components industry
JS local storage
How to perform power regression in R
SAP license: ERP for supply chain management and Implementation
Some knowledge of the beginning of 2022
[JS Framework] Failed to execute the callback function:
JS event details
Recommend 14 commonly used test development tools
Sudoku (easy to understand)
110. balanced binary tree
Sword finger offer 10- ii Frog jumping on steps
Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot