LeetCode-26

Links:https://leetcode.com/problems/remove-duplicates-from-sorted-array/

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:STL模板库,unique()函数,distance()函数.

 

思路2:因为已经排序好了,所以一次遍历替换即可.比前面那个稍快一些…速度都一样。

因为要求不能使用额外空间.所以第二种方法符合题意.但是还是达不到最快的那种…

 

【LeetCode】26. Remove Duplicates from Sorted Array
Tagged on:
0 0 投票数
Article Rating
订阅评论
提醒

0 评论
内联反馈
查看所有评论