当前位置:网站首页>【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.参考代码
边栏推荐
- Selection (030) - what is the output of the following code?
- Digital transformation informatization data planning and technology planning
- Application service access configuration parameters
- Monotone stack template
- Recommend 14 commonly used test development tools
- [golang] leetcode intermediate - jumping game & different paths
- The mixed calculation of rpx and PX in JS by the uniapp applet
- 2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
- Interview algorithm - string question summary
- Why should state-owned enterprises accelerate the digital transformation
猜你喜欢

SAP license: what is ERP supply chain

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

Mcu-08 interrupt system and external interrupt application

SDL: cannot play audio after upgrading openaudio to openaudiodevice

Conception de systèmes de micro - services - construction de sous - services

Three layer switching experiment

How can an enterprise successfully complete cloud migration?

How to select the best test cases for automation?

How do yaml files and zmail collide with the spark of the framework, and how can code and data be separated gracefully?
Online sequence flow chart making tool
随机推荐
Mcu-08 interrupt system and external interrupt application
Three years of bug free, tips for improving code quality
Leetcode question 136 [single number]
Business based precipitation component = & gt; manage-table
Ultimate Guide: comprehensive analysis of log analysis architecture of Enterprise Cloud native PAAS platform
How to perform robust regression in R
Redis learning -- list of redis operations
Eight recommended microservice testing tools
Why should state-owned enterprises accelerate the digital transformation
Self taught C special data type
How to create a linear model prediction interval in R and visualize it
What is business intelligence (BI)?
ASP. Net hosting uploading file message 500 error in IIS
[JS Framework] Failed to execute the callback function:
如何在 R 中创建线性模型预测区间 并可视化
中电投先融期货这家公司怎么样?期货开户办理安全吗?
Three layer switching experiment
Exception: Gradle task assembleDebug failed with exit code 1
Bigdecimalavoiddoubleconstructorrule: do not directly use the double variable as a parameter to construct BigDecimal
JS local storage