当前位置:网站首页>26. delete duplicates of ordered array
26. delete duplicates of ordered array
2022-06-24 10:33:00 【Sit at a sunny window and drink tea alone】



Main idea
- Different adjacent elements can be directly covered , Just keep going down , Use one count Record that the array does not repeat the element subscript at this time
- Every cover ,
count++
Details :

class Solution {
public int removeDuplicates(int[] nums) {
// The following element overrides the previous element , Two adjacent elements with different values are overwritten
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;
}
}
You can see the code above , Boundary part only count and i , about count Come on , The maximum is nums The length of , So it won't cross the border . But for i Come on i Initial value by 0 , And at the beginning 0 No. element should also be put into the array , And after that i > 0 Satisfy Conditions
therefore One more i == 0 The boundary conditions of
边栏推荐
- [resource sharing] the 5th International Conference on civil, architectural and environmental engineering in 2022 (iccaee 2022)
- 消息队列的作用
- 如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
- 机械臂速成小指南(一):机械臂发展概况
- numpy. logical_ or
- 静态链接库和动态链接库的区别
- tf.errors
- 2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)
- 学习整理在php中使用KindEditor富文本编辑器
- np.float32()
猜你喜欢

Role of message queuing

Petit guide de construction rapide du bras mécanique (II): application du bras mécanique

2022 International Symposium on intelligent robots and systems (isoirs 2022)

栈题目:函数的独占时间

24. 图像拼接大作业

Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.

Leetcode-498: diagonal traversal

2. login and exit function development

Spark提交参数--files的使用

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏
随机推荐
Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
抓包工具charles實踐分享
3.员工的增删改查
SSM integration
Leetcode-2221: triangular sum of arrays
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
5. dish management business development
The difference between the sleep () method and the wait () method of a thread
JMeter interface test tool foundation - badboy tool
6.套餐管理业务开发
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
Appium automation test foundation - mobile end test environment construction (I)
Baidu online disk download has been in the process of requesting solutions
Status of the thread pool
抓包工具charles实践分享
leetCode-223: 矩形面积
Record the range of data that MySQL update will lock
tf. errors
numpy. logical_ or
International Symposium on energy and environmental engineering in 2022 (coeee 2022)