当前位置:网站首页>删除有序数组中的重复项 II[双指针--多情况统一]
删除有序数组中的重复项 II[双指针--多情况统一]
2022-06-30 15:48:00 【REN_林森】
前言
很多算法,不需要取模拟,模拟会有很多情况,你就需要很多if,甚至你if不过来,if中有if。
要学会多情况统一处理,让简洁的代码完成才是一条正确的路线。
一、删除有序数组中的重复项 II

二、双指针
package everyday.doublePoint;
public class RemoveDuplicates {
/* target:原地修改数组,让已升序的数组中元素不能超过两个,即1-2个。最后返回前面修改好的有效元素个数。 一个指针指向实际可以赋值的位置,一个指针遍历nums所有元素,把合法的元素都赋值到实际的指针处。 用一个变量记录记录不合法的数字,用nums.length - 不合法个数即为实际长度。 */
public int removeDuplicates(int[] nums) {
// 实际可以赋值的位置。
int real = 0;
// 记录前一个变量是谁,以及已经重复了多少这个变量。
int pre = 1 << 31, sameNum = 0;
// 记录数组的有效长度。
int len = nums.length;
// 第二个指针遍历nums数组,筛选符合条件的数。
for (int i = 0; i < nums.length; i++) {
// 和前面不等,新值,需要重设pre/sameNum,且把符合条件的值赋值到real处。
if (pre != nums[i]) {
pre = nums[i];
sameNum = 1;
nums[real++] = nums[i];
}
// 相同元素还不超过2
else if (sameNum++ < 2) nums[real++] = nums[i];
// 相同元素超过2,无效元素
else --len;
}
return len;
}
}
总结
1)双指针
2)找到一个可以统一的处理方式,就是一种算法。
参考文献
边栏推荐
- Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
- Carry two load balancing notes and find them in the future
- 香港回归25周年 香港故宫博物馆正式开放成文化新地标
- 数据挖掘知识点整理(期末复习版)
- The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
- [Verilog quick start of Niuke online question series] ~ bit splitting and operation
- 备战数学建模34-BP神经网络预测2
- 荣盛生物冲刺科创板:拟募资12.5亿 年营收2.6亿
- Li Zexiang, a legendary Chinese professor, is making unicorns in batches
- RTP 发送PS流零拷贝方案
猜你喜欢

居家办公浅谈远程协助快速提效心得 | 社区征文

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

Which direction should college students choose to find jobs after graduation?
![[machine learning] K-means clustering analysis](/img/5f/3199fbd4ff2129d3e4ea518812c9e9.png)
[machine learning] K-means clustering analysis

How to connect the Internet Reading Notes - Summary

What role does "low code" play in enterprise digital transformation?

香港回归25周年 香港故宫博物馆正式开放成文化新地标

更多龙蜥自研特性!生产可用的 Anolis OS 8.6 正式发布

Mathematical modeling for war preparation 34-bp neural network prediction 2

KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system
随机推荐
为了使远程工作不受影响,我写了一个内部的聊天室 | 社区征文
RT thread heap size Setting
赛芯电子冲刺科创板:拟募资6.2亿 实控人谭健为美国籍
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
What is the difference between real-time rendering and pre rendering
[activity registration] it's your turn to explore the yuan universe! I will be waiting for you in Shenzhen on July 2!
新茶饮“死去活来”,供应商却“盆满钵满”?
Explain in detail the use of for loop, break and continue in go language
2020 Blue Bridge Cup group B - move bricks - (greedy sorting +01 backpack)
MySQL开放远程连接权限的两种方法
【Verilog基础】关于Clock信号的一些概念总结(clock setup/hold、clock tree、clock skew、clock latency、clock transition..)
24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
荣盛生物冲刺科创板:拟募资12.5亿 年营收2.6亿
Mathematical modeling for war preparation 34-bp neural network prediction 2
Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
附加:(还没写,别看~~~)WebMvcConfigurer接口;
Good partner for cloud skill improvement, senior brother cloud of Amazon officially opened today
2022蓝桥杯国赛B组-费用报销-(线性dp|状态dp)
[Verilog basics] octal and hexadecimal representation of decimal negative numbers
Build cloud native observability capability suitable for organizations