Remove Duplicates from Sorted Array
来源:互联网 发布:blueray播放器 mac 编辑:程序博客网 时间:2024/05/17 05:54
1.题目
给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。不要使用额外的数组空间,必须在原地没有额外空间的条件下完成
给出数组A =[1,1,2],你的函数应该返回长度2,此时A=[1,2]。
2.算法
这个题目也比较简单,就是维护一个指针,从前向后扫描,如果当前指针指向的数和扫描数不同,则加到数组中
public int removeDuplicates(int[] nums) { // write your code here if (nums.length == 0) { return 0; } int index = 0; for (int i = 1; i < nums.length; i++) { if (nums[index] != nums[i]) { nums[++index] = nums[i]; } } return index + 1; }
def removeDuplicates(self, A): # write your code here if len(A) == 0: return 0 index = 0 for i in range(1, len(A)): if A[index] != A[i]: index += 1 A[index] = A[i] return index + 1
0 0
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- 守护进程编程方法的示例
- 存储过程,触发器,定时任务的入门
- 程序人生系列二
- Python成长之路(三)——词典、文本文件的输入输出、模块、函数的参数传递
- Java语言基础(八)—— 方法
- Remove Duplicates from Sorted Array
- Android播放器菜单的显示隐藏动画效果
- 为什么月工资时间为21.75
- [编程题]整数与IP地址间的转换
- MyEclipse2014搭建ssh jar包冲突
- PHP中的数组(数据结构)
- Django下的邮件发送!!
- windows下安装xgboost的python包
- yii框架中常用变量、函数使用总结