Leetcode Remove Duplicates型別題目 (python)
26. Remove Duplicates from Sorted Array
解法:two pointers
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
i = 0
for j in range(1,len(nums)):
if nums[i] != nums[j]:
i += 1
nums[i] = nums[j]
return i+1
80. Remove Duplicates from Sorted Array II
解法:two pointers+hashmap
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
nums_dic = collections.defaultdict(int)
nums_dic[nums[0]] += 1
i = 0
for j in range(1,len(nums)):
if nums[i] != nums[j] or (nums[i]==nums[j] and nums_dic[nums[j]] < 2):
nums_dic[nums[j]] += 1
i += 1
nums[i] = nums[j]
return i+1
83. Remove Duplicates from Sorted List
解法
利用連結串列的特殊性,可以直接判斷相鄰元素是否重複並且跳過
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
curr = head
while curr and curr.next:
if curr.val != curr.next.val:
curr = curr.next
else:
curr.next = curr.next.next
return head
82. Remove Duplicates from Sorted List II
解法:
利用while迴圈就可以
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
dummy = ListNode(0,head)
prev = dummy
while head:
if head.next and head.val == head.next.val:
while head.next and head.val == head.next.val:
head = head.next
prev.next = head.next
else:
prev = prev.next
head = head.next
return dummy.next
相關文章
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Remove Duplicates from Sorted List leetcode javaREMLeetCodeJava
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- 【Leetcode】83. Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode-Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- Remove Duplicates from Sorted List II leetcode javaREMLeetCodeJava
- 【Leetcode】82. Remove Duplicates from Sorted List IILeetCodeREM
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted ListREM
- Remove-duplicates-from-sorted-listREM
- Remove-duplicates-from-sorted-arrayREM
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- 26. Remove Duplicates from Sorted ArrayREM
- 83. Remove Duplicates from Sorted ListREM
- [LeetCode] Remove Duplicates from Sorted Array 有序陣列中去除重複項LeetCodeREM陣列
- 82. Remove Duplicates from Sorted List IIREM
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- 資料型別和表示式題目資料型別
- leetcode刷題--Remove Linked List ElementsLeetCodeREM
- 德魯週記02 --Leetcode二分查詢題目型別總結LeetCode型別
- leetcode Remove ElementLeetCodeREM
- [leetcode]remove-elementLeetCodeREM
- Leetcode-Remove ElementLeetCodeREM
- Remove Element leetcode javaREMLeetCodeJava
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- Leetcode 442. Find All Duplicates in an ArrayLeetCode