当前位置:网站首页>Array - fast and slow pointer in one breath
Array - fast and slow pointer in one breath
2022-06-25 02:48:00 【xicheng】
Preface
The last article talked about differential arrays , This article begins with the fast and slow pointer technique of the double pointer technique . in addition , The array has the following knowledge points and skills . 
Ideas
Manipulate an array with two pointers , Usually used in :1. When there are changes to the array and a new array cannot be created .2. A traversal of the implementation requirements .
in addition , Double pointer types have fast and slow pointers , Left and right pointer , Sliding window , Common type .
Speed pointer template
int Slow pointer = 0;
for (int Quick pointer = 0; fast < nums.length; Quick pointer ++) {
if ( The fast pointer satisfies a condition ) {
Slow pointer assignment operation
Slow pointer right movement operation
}
}
Remove duplicate items from an ordered array
leetcode The first 26 topic 
Their thinking
Apply template ,“ The fast pointer satisfies one of the conditions ”: The speed pointer refers to different elements . When the last value is returned , Need to set the slow pointer +1.
Complexity analysis
Time complexity :O(n),n Is the length of the array . Spatial complexity :O(1).
Code
class Solution {
public int removeDuplicates(int[] nums) {
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
// The element indicated by the speed pointer does not work
if (nums[fast] != nums[slow]) {
nums[++slow] = nums[fast];
}
}
return slow + 1;
}
}
Remove elements
leetcode The first 27 topic 
Their thinking
Apply template ,“ The fast pointer satisfies one of the conditions ”: Indicates that the element indicated by the fast pointer is different from the given value .
Complexity analysis
Time complexity :O(n),n Is the length of the array .
Spatial complexity :O(1).
Code
class Solution {
public int removeElement(int[] nums, int val) {
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
// The element indicated by the fast pointer is related to val Different
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}
Move zero
leetcode The first 283 topic
Their thinking
Apply template ,“ The fast pointer satisfies one of the conditions ”: The element indicated by the fast pointer is not equal to 0.
Finally, we traverse the array from the slow pointer , Assign values to all elements traversed 0.
Complexity analysis
Time complexity :O(n),n Is the length of the array .
Spatial complexity :O(1).
Code
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++;
}
}
// Assign the full pointer and the following position to 0
for (int i = slow; i < nums.length; i++) {
nums[i] = 0;
}
}
}
ending
The speed pointer of the array is relatively simple , In addition, linked lists also have fast and slow pointer skills . The next algorithm article talks about the left and right pointers of double pointers .
Wechat scan the QR code below , Or search for “xicheng”, Pay attention to the official account 【 note 】, I have prepared 15 swastika Java Interview notes .
Thank you for your praise 、 Collections and reviews , Dry goods articles are continuously updated , See you in the next article !
边栏推荐
- doak-cms 文章管理系统 推荐
- Use of hashcat
- Can the polardb database be connected to the data source through MySQL
- Refresh mechanism of vie
- leecode学习笔记-机器人走到终点的最短路径
- psql 列转行
- Distributed transaction solutions and code implementation
- Of the seven levels of software testers, it is said that only 1% can achieve level 7
- Intranet learning notes (6)
- 202112-2 序列查询新解
猜你喜欢

电脑端微信用户图片DAT格式解码为图片(TK版)

Test / development programmers, 30, do you feel confused? And where to go

Talking about the advantages of flying book in development work | community essay solicitation

How transformers Roberta adds tokens

Transformers Roberta如何添加tokens

random list随机生成不重复数

计网 | 【四 网络层】知识点及例题

Intranet learning notes (5)

消息称一加将很快更新TWS耳塞、智能手表和手环产品线

【STL源码剖析】STL六大组件功能与运用(目录)
随机推荐
Resolution of cross reference in IDA
F - Spices(线性基)
Mall project pc--- product details page
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
PE文件基础结构梳理
请问polarDB数据库可以通过mysql进行数据源连接吗
AI clothing generation helps you complete the last step of clothing design
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(1)——迁移数据到节点1
DSPACE设置斑马线和道路箭头
Unity存档系统——Json格式的文件
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
Groovy之高级用法
Redis
left join on和 join on的区别
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
Smartctl opens the device and encounters permission denied problem troubleshooting process record
调用系统函数安全方案
Jetson nano from introduction to practice (cases: opencv configuration, face detection, QR code detection)
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(3)—— 把数据库设置为归档模式
记一次beego通过go get命令后找不到bee.exe的坑
