当前位置:网站首页>数组-一口气冲完快慢指针
数组-一口气冲完快慢指针
2022-06-24 23:32:00 【xicheng】
前言
上篇文章讲了差分数组,这篇文章开始讲双指针技巧的快慢指针技巧。另外,数组有下图这些知识点与技巧。
思路
通过两个指针来操作数组,通常应用在:1.对数组有更改且不能建立新数组的情况下。2.一遍历实现需求。
另外,双指针类型有快慢指针,左右指针,滑动窗户,普通类型。
快慢指针模板
int 慢指针 = 0;
for (int 快指针 = 0; fast < nums.length; 快指针++) {
if (快指针满足某个条件) {
慢指针赋值操作
慢指针右移动操作
}
}
删除有序数组中的重复项
leetcode第26题解题思路
套用模板,“快指针满足某个条件中的条件”:指快慢指针所指的元素不同。 最后返回值时,需要将慢指针+1。
复杂度分析
时间复杂度:O(n),n是数组的长度。 空间复杂度:O(1)。
代码
class Solution {
public int removeDuplicates(int[] nums) {
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
//快慢指针所指元素不通
if (nums[fast] != nums[slow]) {
nums[++slow] = nums[fast];
}
}
return slow + 1;
}
}
移除元素
leetcode第27题解题思路
套用模板,“快指针满足某个条件中的条件”:指快指针所指的元素与给定值不同。
复杂度分析
时间复杂度:O(n),n是数组的长度。
空间复杂度:O(1)。
代码
class Solution {
public int removeElement(int[] nums, int val) {
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
//快指针所指元素与val不同
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}
移动零
leetcode第283题解题思路
套用模板,“快指针满足某个条件中的条件”:指快指针所指的元素不等于0。
最后从在慢指针往后遍历数组,将遍历到的元素都赋值为0。
复杂度分析
时间复杂度:O(n),n是数组的长度。
空间复杂度:O(1)。
代码
class Solution {
public void moveZeroes(int[] nums) {
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
if (nums[fast] != 0) {
nums[slow] = nums[fast];
slow++;
}
}
//将满指针及后面的位置都赋值为0
for (int i = slow; i < nums.length; i++) {
nums[i] = 0;
}
}
}
结尾
数组的快慢指针比较简单,另外链表也有快慢指针技巧。下一篇算法文章讲双指针之左右指针。
微信扫描下方二维码,或搜索“xicheng”,关注公众号后回复【笔记】,有我准备的15万字Java面试笔记。
感谢各位人才的点赞、收藏和评论,干货文章持续更新中,下篇文章再见!
边栏推荐
- How transformers Roberta adds tokens
- When they are in private, they have a sense of propriety
- 记一次beego通过go get命令后找不到bee.exe的坑
- NPM package publishing tutorial
- Can automate - 10k, can automate - 20K, do you understand automated testing?
- 记一次beego通过go get命令后找不到bee.exe的坑
- Advanced usage of groovy
- ACL access control of squid proxy server
- 3年测试经验,连简历上真正需要什么都没搞明白,张口就要20k?
- Charles packet capturing tool
猜你喜欢
PyTorch学习笔记(七)------------------ Vision Transformer
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
电脑端微信用户图片DAT格式解码为图片(TK版)
[analysis of STL source code] functions and applications of six STL components (directory)
random list随机生成不重复数
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
Detailed explanation of cache (for the postgraduate entrance examination of XD)
LeetCode 210:课程表 II (拓扑排序)
[STL source code analysis] configurator (to be supplemented)
高数 | 精通中值定理 解题套路汇总
随机推荐
电脑端微信用户图片DAT格式解码为图片(TK版)
数据库系统概论必背知识
Detailed explanation of cache (for the postgraduate entrance examination of XD)
Can automate - 10k, can automate - 20K, do you understand automated testing?
What is the reason for the disconnection of video playback due to the EHOME protocol access of easycvr platform?
背了八股文,六月赢麻了……
Distributed transaction solutions and code implementation
中信证券手机开户是靠谱的吗?安全吗
How to monitor the log through the easycvr interface to observe the platform streaming?
Left hand dreams right hand responsibilities GAC Honda not only pays attention to sales but also children's safety
一线城市软件测试工资——你拖后腿了吗
Experience of epidemic prevention and control, home office and online teaching | community essay solicitation
Talking about the advantages of flying book in development work | community essay solicitation
3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(3)—— 把数据库设置为归档模式
李宏毅《机器学习》丨6. Convolutional Neural Network(卷积神经网络)
常用的软件测试工具清单,请查收。
vim的Dirvish中文文档
[I.MX6UL] U-Boot移植(六) 网络驱动修改 LAN8720A
Summary of stack frame in arm assembly