一、题目
二、示例
三、代码
代码1:
class Solution:
def search(self , nums: List[int], target: int) -> int:
if target not in nums:
return -1
return nums.index(target)
代码2:
class Solution:
def search(self , nums: List[int], target: int) -> int:
left, right = 0, len(nums)-1
while left <= right:
temp = (left + right) // 2
if nums[temp] == target:
return temp
elif nums[temp] > target:
right = temp - 1
else:
left = temp + 1
return -1
四、算法说明
代码1:投机取巧。
代码2:二分法查找,具体过程不再赘述。
胡萝卜
2022年9月2日
我不知道将去向何方,但我已在路上! |
---|
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! |