lintcode
程序员文章站
2022-07-15 12:15:53
...
二分查找
给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。
class Solution:
"""
@param nums: The integer array.
@param target: Target to find.
@return: The first position of target. Position starts from 0.
"""
def binarySearch(self, nums, target):
# write your code here
for i in range(len(nums)):
if nums[i] == target:
return i
return -1
上一篇: 【Lintcode】945. Task Scheduler
下一篇: 任务计划