Remove Duplicates from Sorted Array

題目

Given a sorted array nums, remove the duplicates in-placearrow-up-right such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-placearrow-up-right with O(1) extra memory.

Given 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 returned length.
Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

思路

  • 有序陣列,利用兩個快慢指標來處理。

  • 慢指針用來指向當前不重複的index。

  • 快指針用來循環掃過整個loop。

程式

複雜度

  • 時間: O(n) n: 陣列長度

    • fast指針至少要循環掃過array一遍。

  • 空間: O(1)

    • 額外空間給予slow指針。

Last updated