当前位置:网站首页>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
边栏推荐
- The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
- CGR 21 (D,E,F)
- What are the reasons for the errors reported by the Flink SQL CDC synchronization sqlserver
- 观测云与 TDengine 达成深度合作,优化企业上云体验
- RT thread heap size Setting
- Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan
- JS Es5 can also create constants?
- [machine learning] K-means clustering analysis
- 今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
- 19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
猜你喜欢

新茶饮“死去活来”,供应商却“盆满钵满”?

Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan

Observation cloud reached in-depth cooperation with tdengine to optimize enterprise cloud experience

MySQL transaction / lock / log summary

备战数学建模35-时间序列预测模型
mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法

Niuke network: longest continuous subarray with positive product

What is the difference between real-time rendering and pre rendering

halcon知识:区域专题【07】

7 月 2 日邀你来TD Hero 线上发布会
随机推荐
Halcon knowledge: regional topics [07]
What is the difference between real-time rendering and pre rendering
MySQL8.0开启远程连接权限的方法步骤
备战数学建模36-时间序列模型2
猎头5万挖我去VC
I implement "stack" with C I
优惠券种类那么多,先区分清楚再薅羊毛!
7 月 2 日邀你来TD Hero 线上发布会
观测云与 TDengine 达成深度合作,优化企业上云体验
赛芯电子冲刺科创板:拟募资6.2亿 实控人谭健为美国籍
【微信小程序】常用组件基本使用(view/scroll-view/swiper、text/rich-text、button/image)
微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
云技能提升好伙伴,亚马逊云师兄今天正式营业
On July 2, I invited you to TD Hero online conference
[Verilog basics] octal and hexadecimal representation of decimal negative numbers
halcon知识:矩阵专题【02】
Observation cloud reached in-depth cooperation with tdengine to optimize enterprise cloud experience
What are the reasons for the errors reported by the Flink SQL CDC synchronization sqlserver
更多龙蜥自研特性!生产可用的 Anolis OS 8.6 正式发布
Mysql8.0 method and steps for enabling remote connection permission