LC 704: 二分查找
- 思路
- example
- 元素不重复
- 已经排序的数组
- 二分模板1,左闭右闭, <, ==, >, while left <= right, quit when left > right
- left, right = 0, n-1
- 复杂度
- 时间:O(log n)
- 空间: O(1)
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
left, right = 0, n-1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid -1
else:
return mid
# left > right, possibly left = n, right = -1
return -1
- 二分模板2,左闭右开, while left < right, quit when left == right
- left, right = 0, n
class Solution:
def search(self, nums: List[int], target: int) -> int:
n = len(nums)
left, right = 0, n
while left < right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else: # nums[mid] > target
right = mid
return -1
- 进阶:二分查找 左右边界
LC 27. 移除元素
- 思路
- example
- 暴力,time:
- 双指针,in-place; -->-->, slow/fast, 相同起点
- outside loop: fast (right)
- inside: slow (left)
- 根据fast指针指向状态,更新结果,slow指针右移
- 复杂度: 时间:O(n), 空间: O(1)
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
n = len(nums)
slow, fast = 0, 0
while fast < n:
if nums[fast] != val:
nums[slow] = nums[fast]
slow += 1
fast += 1
return slow
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
n = len(nums)
left, right = 0, 0
while right < n:
if nums[right] != val:
nums[left] = nums[right]
left += 1
right += 1
return left
26. Remove Duplicates from Sorted Array
- 思路
- example
- 暴力,time:
- 双指针,in-place; -->-->, slow/fast, 相同起点
-
复杂度: 时间:O(n), 空间: O(1)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n = len(nums)
slow, fast = 0, 1 # fast 也可以取0
while fast < n:
if nums[fast] != nums[slow]:
slow += 1
nums[slow] = nums[fast]
fast += 1
return slow + 1
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n = len(nums)
left, right = 1, 1
while right < n:
if nums[right] != nums[right-1]:
nums[left] = nums[right]
left += 1
right += 1
return left
83. Remove Duplicates from Sorted List
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head == None:
return None
slow, fast = head, head
while fast != None:
if fast.val != slow.val:
slow.next = fast # 链接
slow = slow.next
fast = fast.next
slow.next = None # 别忘了这个
return head
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummyhead = ListNode(float('inf'), head)
pre, cur = dummyhead, head
while cur:
if cur.val == pre.val:
pre.next = cur.next # 删除
else:
pre = pre.next
cur = cur.next
return dummyhead.next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummyhead = ListNode(float('inf'), head)
pre, cur = dummyhead, head
while cur:
if cur.val == pre.val:
pre.next = cur.next
cur = pre.next
else:
pre = cur
cur = cur.next
return dummyhead.next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head == None:
return None
pre = head
cur = head.next
while cur:
if cur.val == pre.val:
pre.next = cur.next
else:
pre = cur
cur = cur.next
return head