当前位置:网站首页>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 !
边栏推荐
猜你喜欢

Software testing salary in first tier cities - are you dragging your feet

yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
![[STL source code analysis] configurator (to be supplemented)](/img/87/0ed1895e9cdb5327411c0c9cb0197f.png)
[STL source code analysis] configurator (to be supplemented)

都2022年了,你还不了解什么是性能测试?

After reciting the eight part essay, I won the hemp in June

折叠屏将成国产手机分食苹果市场的重要武器

Sumati gamefi ecological overview, element design in the magical world

自动化测试

How transformers Roberta adds tokens

ProcessOn制作ER过程(自定义)
随机推荐
都2022年了,你还不了解什么是性能测试?
折叠屏将成国产手机分食苹果市场的重要武器
Dirvish Chinese document of vim
AI clothing generation helps you complete the last step of clothing design
一线城市软件测试工资——你拖后腿了吗
Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups
Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
Go synchronization waiting group
centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
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
Modifying universal render data at runtime
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
Once beego failed to find bee after passing the go get command Exe's pit
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
Folding screen will become an important weapon for domestic mobile phones to share the apple market
MATLAB主窗口与编辑器窗口分开为两个界面的解决办法
ida中交叉引用的解析
Talking about the advantages of flying book in development work | community essay solicitation
Of the seven levels of software testers, it is said that only 1% can achieve level 7
