Leetcode - array

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

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

排序而且有重复,那么

  • 记录下一个不重复的数值的位置,并自动向后移
  • 找到其中新的不重复数值。或者单独一个值记录,或者根据前后是否发生变化

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?

For example, given sorted array A = [1,1,1,2,2,3], your function should return length = 5, and A is now [1,1,2,2,3]

你可能感兴趣的:(面试准备,leetcode,算法,职场和发展)