#左闭右闭
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:
left = mid +1
elif nums[mid] > target:
right = mid -1 #右闭
else:
return mid
return -1
#左闭右开
class Solution:
def search(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums) #右开
while left < right:
mid = (left+right) //2
if nums[mid] < target:
left = mid +1
elif nums[mid] > target:
right = mid #右开
else:
return mid
return -1
27.移除元素. - 力扣(LeetCode)
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
a = 0
b = 0
while a < len(nums):
if nums[a] != val:
nums[b] = nums[a]
b+=1
a +=1
return b