From a25e6cf93394e86f4bc3fc5262ed1f38e3531436 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Sat, 13 Aug 2022 13:47:10 -0400 Subject: [PATCH] Time: 71 ms (88.49%), Space: 14.6 MB (77.98%) - LeetHub --- .../26-remove-duplicates-from-sorted-array.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 26-remove-duplicates-from-sorted-array/26-remove-duplicates-from-sorted-array.py diff --git a/26-remove-duplicates-from-sorted-array/26-remove-duplicates-from-sorted-array.py b/26-remove-duplicates-from-sorted-array/26-remove-duplicates-from-sorted-array.py new file mode 100644 index 0000000..6d1c998 --- /dev/null +++ b/26-remove-duplicates-from-sorted-array/26-remove-duplicates-from-sorted-array.py @@ -0,0 +1,19 @@ +class Solution(object): + def removeDuplicates(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + oldIndex = 1 + newIndex = 1 + lastNum = nums[0] + + while oldIndex < len(nums): + if nums[oldIndex] == lastNum: + oldIndex += 1 + else: + nums[newIndex] = nums[oldIndex] + newIndex += 1 + lastNum = nums[oldIndex] + + return len(nums[:newIndex]) \ No newline at end of file