Description
Difficulty: Easy
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.
题意:
原地为一个数组去重,返回不重复的数组长度 n 并且仅需保持该数组前 n 位为不重复的数组。
Solution
虽然效率偏低,但是重复的删掉就好。
|
|
update:
讨论中展示了一种简洁而优美的写法:
|
|
也就是将每一个不同的元素复制到 newTail 位置。





近期评论