当前位置:网站首页>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;
}
}
}边栏推荐
- Numpy 中的方法汇总
- 2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
- 数据中台:中台实践与总结
- liunx服务器 telnet 带用户名 端口登陆方法
- Shell array
- Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
- uniapp 热更新后台管理
- 基于单片机开发的酒精浓度测试仪方案
- Win11 blank when using VIM to view content in cmder
- “不平凡的代理初始值设定不受支持”,出现的原因及解决方法
猜你喜欢

K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream

数据中台:中台架构及概述

The form image uploaded in chorme cannot view the binary image information of the request body

JS to find and update the specified value in the object through the key

2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)

Base64编码详解及其变种(解决加号在URL变空格问题)

什么是SRE?一文详解SRE运维体系
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

为什么ping不通,而traceroute却可以通

À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
随机推荐
Picture tools
项目部署相关
【量化投资】离散傅里叶变换求数组周期
Liunx change the port number of vsftpd
2022春招面试总结
Qt源码分析--QObject(2)
提高INSERT速度
Pymysql inserts data into MySQL and reports an error for no reason
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
mysql组合索引的有序性
Introduction to data platform
MyCAT读写分离与MySQL主从同步
K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
【NOI模拟赛】摆(线性代数,杜教筛)
数据中台:中台架构及概述
数组相向指针系列
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
Using sonar for code checking
Ordering of MySQL composite index
【NOI模拟赛】给国与时光鸡(构造)