mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2026-01-25 07:34:05 +00:00
let's try this again shall we
problems 1->10 of LC150
This commit is contained in:
19
26-remove-duplicates-from-sorted-array/soln.py
Normal file
19
26-remove-duplicates-from-sorted-array/soln.py
Normal file
@@ -0,0 +1,19 @@
|
||||
class Solution:
|
||||
def removeDuplicates(self, nums: list[int]) -> int:
|
||||
# naive, hashmap
|
||||
# cnts = {}
|
||||
# k = 0
|
||||
# for x in nums:
|
||||
# if cnts.get(x) == None:
|
||||
# k += 1
|
||||
# cnts[x] = cnts.get(x, 0) + 1
|
||||
# for idx, key in enumerate(cnts.keys()):
|
||||
# nums[idx] = key
|
||||
# return k
|
||||
# two pointer
|
||||
last_unique = 0
|
||||
for fast in range(1, len(nums)):
|
||||
if nums[last_unique] != nums[fast]:
|
||||
last_unique += 1
|
||||
nums[last_unique] = nums[fast]
|
||||
return last_unique + 1 # zero-indexed
|
||||
Reference in New Issue
Block a user