Monday, March 10, 2014

LeetCode: Remove Duplicates from Sorted Array I&&II



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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].



Solution:

惊艳!One time pass.....


上面的这种方法在解决II的时候变得无比dumb. 其实还有更简单漂亮的解法 双指针 can be considered as double pointers problem

No comments:

Post a Comment