数组篇
704. 二分查找
文章来源:https://www.toymoban.com/news/detail-822480.html
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. 移除元素
文章来源地址https://www.toymoban.com/news/detail-822480.html
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
#快慢指针
#没碰到删除的值,快慢指针都移动,并且赋值
#碰到删除的值,只动快指针,并且不赋值
到了这里,关于代码随想录刷题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!