当前位置:网站首页>26.删除有序数组的重复项
26.删除有序数组的重复项
2022-06-24 09:47:00 【兀坐晴窗独饮茶】



主体思路
- 相邻元素不同直接覆盖即可, 相同就继续往下面走, 使用一个 count 记录此时数组不重复元素下标
- 每覆盖一次,
count++
细节问题 :

class Solution {
public int removeDuplicates(int[] nums) {
// 后面的元素覆盖前面的元素, 相邻两个元素值不同就进行覆盖
int count = 0;
for(int i = 0; i < nums.length; i++) {
if (i == 0 || nums[i] != nums[i - 1]) {
nums[count] = nums[i];
count++;
}
}
return count;
}
}
可以看到上面的代码 , 边界部分只有 count 和 i , 对于 count 来说, 最大值是 nums 的长度, 所以不会越界。但是对于 i 来说 i 初始值 为 0 , 且最开始时的 0 号元素也要放入数组 , 并且在之后i > 0满足 条件
所以 要加一个 i == 0 的边界条件
边栏推荐
猜你喜欢

抓包工具charles实践分享

Leetcode-223: rectangular area

Baidu online disk download has been in the process of requesting solutions

6.套餐管理业务开发

The difference between the sleep () method and the wait () method of a thread

Troubleshooting steps for Oracle pool connection request timeout

Six states of threads

6. package management business development

uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。

Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
随机推荐
SQL Server AVG函数取整问题
uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
整理接口性能优化技巧,干掉慢代码
uniapp实现禁止video拖拽快进
Leetcode-1089: replication zero
p5.js实现的炫酷交互式动画js特效
用扫描的方法分发书稿校样
Baidu online disk download has been in the process of requesting solutions
小程序 rich-text中图片点击放大与自适应大小问题
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
Leetcode interview question 16.06: minimum difference
6.套餐管理业务开发
numpy. linspace()
Learn to use the phpstripslush function to remove backslashes
顺丰科技智慧物流校园技术挑战赛(2022/06/19)【AK】
24. 图像拼接大作业
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法