LeetCode第26/27题:Remove Duplicates from Sorted Array和Remove Element 总结

题目一

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

  • 大意:给定一个已排序的数组,在原数组上移除重复元素,让每个元素只出现一次,返回新数组的长度。(注意:不要申请新数组,必须用原数组的常数空间)比如:输入[1, 1, 2],返回length = 2

思路

注意实在原位修改,所以不能定义一个列表然后append。其实我们可以直接在原列表从0开始记录,因为遍历过程中,不可能出现所修改的元素位置比所遍历的位置靠后的情况。

代码

(Python)

题目二

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

  • 大意: 和上个题目类似,只不过这次去除的是指定的某个元素

思路0

类似上个题目,这次是判断遍历元素是不是指定元素

代码

(Python)

思路1

对于Python,可以直接用remove操作,删除指定元素。

代码

(Pyhton)

参考:

《del remove 和 pop的区别》( http://novell.me/master-diary/2014-06-05/difference-between-del-remove-and-pop-on.html )

发表评论

电子邮件地址不会被公开。 必填项已用*标注