接着上一篇 向数组中插入元素 而来,下面这两道题是从数组中删除删除元素,解法一样:Remove Element 和 Remove Duplicates from Sorted Array。我还是太菜了,居然做不出来……看了 solution 之后才恍然大悟,原来如此,我把两个指针一前一后放置,然后向中间靠拢,但是后面那个指针的移动太快了,因此导致出错,真是『步子大了,喀嚓,容易扯着蛋』。
Remove Element:
Given an array nums and a value
val
, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place withO(1)
extra memory. The order of elements can be changed. It doesn’t matter what you leave beyond the new length.example:
1
2
3
4
5 > Input: nums = [3,2,2,3], val = 3
> Output: 2, nums = [2,2]
> Explanation: Your function should return length = 2, with the first two elements of nums being 2.
> It doesn't matter what you leave beyond the returned length. For example if you return 2 with nums = [2,2,3,3] or nums = [2,2,0,0], your answer will be accepted.
>
Remove Duplicates from Sorted Array:
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
example:
1
2
3
4 > Input: nums = [1,1,2]
> Output: 2, nums = [1,2]
> Explanation: 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 returned length.
>
题解如下所示:
1 | int removeElement(int* nums, int numsSize, int val){ |
1 | int removeDuplicates(int* nums, int numsSize){ |