数组篇
704. 二分查找
class Solution:
def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1
while(left <= right):
mid = (left + right) // 2
if nums[mid] == target:
return mid
elif nums[mid] > target:
right = mid - 1
elif nums[mid] < target:
left = mid + 1
return -1
27. 移除元素
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i = 0
j = 0
while(j < len(nums)):
if nums[j] != val:
nums[i] = nums[j]
i += 1
j += 1
return i
#快慢指针
#没碰到删除的值,快慢指针都移动,并且赋值
#碰到删除的值,只动快指针,并且不赋值