34.在排序数组中查找元素的第一个和最后一个位置

实在不想O(logn)了,就O(n)吧

class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        r = [-1, -1]
        for i, num in enumerate(nums):
            if num == target:
                r[1] = i
                if r[0] == -1:
                    r[0] = i
            if num > target:
                break
        return r

评论