当前位置:网站首页>Delete duplicates in an ordered array ii[double pointers -- unified in multiple cases]
Delete duplicates in an ordered array ii[double pointers -- unified in multiple cases]
2022-06-30 16:48:00 【REN_ Linsen】
Double pointer
Preface
A lot of algorithms , There is no need to take simulation , The simulation will have a lot of situations , You need a lot if, Even you if Don't come here ,if There is if.
Learn to handle multiple situations in a unified way , Let simple code complete is the right way .
One 、 Remove duplicate items from an ordered array II

Two 、 Double pointer
package everyday.doublePoint;
public class RemoveDuplicates {
/* target: Modify array in place , The number of elements in an ascending array cannot exceed two , namely 1-2 individual . Finally, return the number of valid elements modified previously . A pointer to the position that can actually be assigned , A pointer traverses nums All the elements , Assign all legal elements to the actual pointer . Use a variable record to record illegal numbers , use nums.length - The illegal number is the actual length . */
public int removeDuplicates(int[] nums) {
// The position that can actually be assigned .
int real = 0;
// Record who the previous variable is , And how much of this variable has been repeated .
int pre = 1 << 31, sameNum = 0;
// Record the effective length of the array .
int len = nums.length;
// The second pointer traverses nums Array , Filter the number of qualified .
for (int i = 0; i < nums.length; i++) {
// Not equal to the front , The new value , Reset required pre/sameNum, And assign the qualified value to real It's about .
if (pre != nums[i]) {
pre = nums[i];
sameNum = 1;
nums[real++] = nums[i];
}
// The same element does not exceed 2
else if (sameNum++ < 2) nums[real++] = nums[i];
// The same element exceeds 2, Invalid elements
else --len;
}
return len;
}
}
summary
1) Double pointer
2) Find a uniform way to deal with , It's an algorithm .
reference
[1] LeetCode Remove duplicate items from an ordered array II
边栏推荐
- 2022蓝桥杯国赛B组-费用报销-(线性dp|状态dp)
- Niuke network: longest continuous subarray with positive product
- Bidding announcement: Tianjin housing provident fund management center database all-in-one machine and database software project (budget: 6.45 million)
- 2020蓝桥杯国赛B组-搬砖-(贪心排序+01背包)
- MicroBlaze serial port learning · 2
- CGR 21 (D,E,F)
- POJ Project Summer
- Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan
- [download attached] installation and use of penetration test artifact Nessus
- 快照和备份
猜你喜欢

RT-Thread 堆區大小設置
MySQL8.0开启远程连接权限的方法步骤

Anaconda下安装Jupyter notebook

华为帐号多端协同,打造美好互联生活

halcon知识:区域专题【07】

The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer

On July 2, I invited you to TD Hero online conference

容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态

构建适合组织的云原生可观测性能力

JS Es5 can also create constants?
随机推荐
详解Go语言中for循环,break和continue的使用
Etcd教程 — 第八章 Etcd之Compact、Watch和Lease API
香港回归25周年 香港故宫博物馆正式开放成文化新地标
中航无人机科创板上市:市值385亿 拳头产品是翼龙无人机
2020 Blue Bridge Cup group B - move bricks - (greedy sorting +01 backpack)
Raft介绍
新茶饮“死去活来”,供应商却“盆满钵满”?
Additional: (not written yet, don't look at ~ ~ ~) webmvcconfigurer interface;
Installing jupyter notebook under Anaconda
What is the difference between real-time rendering and pre rendering
POJ Project Summer
招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态
Arcmap操作系列:80平面转经纬度84
AVIC UAV technology innovation board is listed: the fist product with a market value of 38.5 billion is pterodactyl UAV
Bidding announcement: Taizhou Unicom Oracle all in one machine and database maintenance service project in 2022
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
2022 Blue Bridge Cup group B -2022- (01 backpack to calculate the number of schemes)
IndexSearch
[activity registration] it's your turn to explore the yuan universe! I will be waiting for you in Shenzhen on July 2!