当前位置:网站首页>Fast and slow pointer series
Fast and slow pointer series
2022-06-24 08:51:00 【accumulate steadily ض】
Double finger needling
- Use Speed pointer
- The fast pointer is responsible for finding the elements of our new array ( Also is val The elements of )
- The slow pointer is responsible for maintaining this new array
class Solution {
public int removeElement(int[] nums, int val) {
int fast = 0;// The fast pointer is responsible for finding the elements of our new array ( Also is val The elements of )
int slow = 0;// The slow pointer is responsible for maintaining this new array
for(fast =0;fast<nums.length;++fast){
if(nums[fast]!=val){// If it is found that the value pointed by the fast pointer is not val, Then you need to assign this value to the location to be updated , After the assignment slow Go back and continue to point to the location to be updated to maintain the new array
nums[slow++] = nums[fast];
}
}
return slow;
}
}Violent solution :
- Use two for Loop through , The outer loop iterates through each element of the array , Inner loops are used to move elements ( To cover )
- If the array element is val That requires an inner loop to move elements
class Solution {
public int removeElement(int[] nums, int val) {
int length = nums.length;
for(int i=0;i<length;++i){
if(nums[i]==val){
for(int j =i+1;j<length;++j){
nums[j-1]= nums[j];
}
i--;
length--;
}
}
return length;
}
}26. Remove duplicate items from an ordered array
- Use Speed pointer
- fast To find the elements of a new array , That is, the element that does not repeat the previous one
- slow Used to maintain new arrays
class Solution {
public int removeDuplicates(int[] nums) {
// Use the speed pointer
// still fast To find the elements of a new array , That is, the element that does not repeat the previous one
//slow Used to maintain new arrays
int fast =0;
int slow =0;
for(fast =1;fast<nums.length;++fast){
int tmp = nums[fast-1];// Record fast The elements in front
if(nums[fast]!=tmp){
// Different from the elements of the record , Proof of need to join slow In the new array maintained
nums[++slow] = nums[fast];
}
}
return slow+1;
}
}- Use Speed pointer
- fast To find the elements of a new array , That is, the element that does not repeat the previous one
- slow Used to maintain new arrays
- Last slow The new array previously maintained for us , The back is all set 0
class Solution {
public void moveZeroes(int[] nums) {
int fast =0;
int slow =0;
for(fast =0;fast<nums.length;++fast){
if(nums[fast]!=0){
nums[slow++] = nums[fast];
}
}
for(int i = slow;i<nums.length;++i){
nums[i] = 0;
}
}
}边栏推荐
- leetcode 1268. Search suggestions system
- Why can ping fail while traceroute can
- OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
- Variable declaration and some special variables in shell
- String to Base64
- Jenkins is deployed automatically and cannot connect to the dependent service [solved]
- 一文讲透,商业智能BI未来发展趋势如何
- 【Pytorch基础教程31】YoutubeDNN模型解析
- 提高INSERT速度
- 利用ngrok做内网穿透
猜你喜欢
![Jenkins is deployed automatically and cannot connect to the dependent service [solved]](/img/fe/f294955a9bdf7492aab360e44e052d.png)
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)

教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5

Xiaohei ai4code code baseline nibble 1

K8S部署高可用postgresql集群 —— 筑梦之路

什么是SRE?一文详解SRE运维体系

Telnet port login method with user name for liunx server

一文讲透,商业智能BI未来发展趋势如何

WebRTC系列-网络传输之5选择最优connection切换
随机推荐
基于QingCloud的地理信息企业研发云解决方案
What is SRE? A detailed explanation of SRE operation and maintenance system
【NOI模拟赛】给国与时光鸡(构造)
Shell basic operator -- arithmetic operator
Using ngrok for intranet penetration
WebRTC系列-网络传输之5选择最优connection切换
Background management of uniapp hot update
关于ETL看这篇文章就够了,三分钟让你明白什么是ETL
何时使用RDD和DataFrame/DataSet
Rsync for file backup
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
【量化投资】离散傅里叶变换求数组周期
liunx 更改 vsftpd 的端口号
One development skill a day: how to establish P2P communication based on webrtc?
every()、map()、forEarch()方法。数组里面有对象的情况
小程序云数据,数据请求一个集合数据的方法
Battle history between redis and me under billion level traffic
Qt 中发送自定义事件