当前位置:网站首页>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 !
边栏推荐
- LeetCode 210:课程表 II (拓扑排序)
- PyTorch学习笔记(七)------------------ Vision Transformer
- Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
- 【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
- left join on和 join on的区别
- Charles 抓包工具
- ACM. HJ70 矩阵乘法计算量估算 ●●
- How to uninstall CUDA
- 商城项目 pc----商品详情页
- PSQL column to row
猜你喜欢

Redis

Solution of separating matlab main window and editor window into two interfaces

记一次beego通过go get命令后找不到bee.exe的坑

Leecode learning notes - the shortest path for a robot to reach its destination

leecode学习笔记-机器人走到终点的最短路径

left join on和 join on的区别

14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容

Pytorch learning notes (VII) ------------------ vision transformer

Unity存档系统——Json格式的文件

DDD concept is complex and difficult to understand. How to design code implementation model in practice?
随机推荐
Unity archive system - file in JSON format
centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
入坑机器学习:一,绪论
3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
软件测试周刊(第77期):只要放弃一次,就会滋生放弃的习性, 原本可以解决的问题也会变得无法解决。
F - spices (linear basis)
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (3) -- set the database to archive mode
请问polarDB数据库可以通过mysql进行数据源连接吗
Use of hashcat
js正则匹配数字、大小写字母、下划线、中线和点[通俗易懂]
Intranet learning notes (7)
How transformers Roberta adds tokens
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
Dirvish Chinese document of vim
internship:svn的使用
Intranet learning notes (5)
Rod and Schwartz cooperated with ZhongGuanCun pan Lianyuan Institute to carry out 6G technology research and early verification
PyTorch学习笔记(七)------------------ Vision Transformer
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (4) -- modify the scanip of Oracle11g RAC cluster
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
