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 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM
- Remove-duplicates-from-sorted-arrayREM
- Remove-duplicates-from-sorted-listREM
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [leetcode]remove-elementLeetCodeREM
- 德魯週記02 --Leetcode二分查詢題目型別總結LeetCode型別
- Leetcode 442. Find All Duplicates in an ArrayLeetCode
- 資料型別和表示式題目資料型別
- leetcode-27. Remove ElementLeetCodeREM
- Leetcode 27 Remove-ElementLeetCodeREM
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- LeetCode題解(1512):好數對的數目(Python)LeetCodePython
- Leetcode(Python3) 19. Remove Nth Node From End of ListLeetCodePythonREM
- dfs題目這樣去接題,秒殺leetcode題目LeetCode
- [LeetCode] 402. Remove K DigitsLeetCodeREMGit
- 【LeetCode】簡單題目集LeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- 【Leetcode】題目集:31-40LeetCode
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- Python題目集Python
- leetcode題目解析(js)--連結串列LeetCodeJS
- LeetCode題解(1621):大小為K的不重疊線段的數目(Python)LeetCodePython
- LeetCode題解(1639):統計只差一個字元的子串數目(Python)LeetCode字元Python
- python階段題目Python
- 分享一道有趣的 Leetcode 題目LeetCode
- LeetCode BFS題目以及要注意的點LeetCode
- Leetcode目錄LeetCode
- LeetCode數學問題(Python)LeetCodePython
- LeetCode:動態規劃+貪心題目整理LeetCode動態規劃
- leetcode題目10之正規表示式匹配LeetCode
- LeetCode簡單演算法題目-JS解法LeetCode演算法JS