当前位置:网站首页>删除有序数组中的重复项 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)找到一个可以统一的处理方式,就是一种算法。
参考文献
边栏推荐
- 几百行代码实现一个 JSON 解析器
- Two methods for MySQL to open remote connection permission
- Li Zexiang, a legendary Chinese professor, is making unicorns in batches
- After 15 years of working on 21 types of hardware, where is Google?
- RT-Thread 堆區大小設置
- Arcmap操作系列:80平面转经纬度84
- RTP 发送PS流零拷贝方案
- OpenCV中LineTypes各枚举值(LINE_4 、LINE_8 、LINE_AA )的含义
- CVPR 2022 - Tesla AI proposed: generalized pedestrian re recognition based on graph sampling depth metric learning
- Halcon knowledge: matrix topic [02]
猜你喜欢
构建适合组织的云原生可观测性能力
Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"
RT thread heap size setting
I implement "stack" with C I
15年做糊21款硬件,谷歌到底栽在哪儿?
[Verilog basics] summary of some concepts about clock signals (clock setup/hold, clock tree, clock skew, clock latency, clock transition..)
牛客网:有多少个不同的二叉搜索树
数据挖掘知识点整理(期末复习版)
随机推荐
RTP 发送PS流零拷贝方案
RT thread heap size Setting
Solution for IIS failing to load font files (*.woff, *.svg)
MySQL开放远程连接权限的两种方法
Mathematical modeling for war preparation 34-bp neural network prediction 2
[Verilog quick start of Niuke online question series] ~ bit splitting and operation
【机器学习】K-means聚类分析
php7.3 centos7.9安装sqlserver扩展
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
为了使远程工作不受影响,我写了一个内部的聊天室 | 社区征文
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)
Which direction should college students choose to find jobs after graduation?
备战数学建模35-时间序列预测模型
Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan
15年做糊21款硬件,谷歌到底栽在哪儿?
Li Zexiang, a legendary Chinese professor, is making unicorns in batches
牛客网:乘积为正数的最长连续子数组
halcon知识:区域专题【07】
2022蓝桥杯国赛B组-费用报销-(线性dp|状态dp)
Build cloud native observability capability suitable for organizations