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

题目链接 :https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/

题目描述:

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

你的算法时间复杂度必须是 O(log n) 级别。

如果数组中不存在目标值,返回 [-1, -1]

示例:

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: [3,4]

示例 2:

输入: nums = [5,7,7,8,8,10], target = 6
输出: [-1,-1]

思路:

思路1:手动二分法!

这里提供两种方法,一种容易理解,一种万能公式!

版本1:是指在二分法进行时,就判读是否有等于target的,但是找到的target不知道是最左边的数还是最右边的数,所以,通过找到这个数再找出相应的边界值.

版本2:是指二分法执行完毕,返回target在最左边的位置,在求出另一个边界!

关于详细说明,请看这篇[二分搜索](二分查找有几种写法?它们的区别是什么? - Jason Li的回答 - 知乎
https://www.zhihu.com/question/36132386/answer/530313852),读完之后,可以加深二分搜索的理解!

思路2:库函数

python 的 bisect库

简要介绍一下,

bisect.bisect_left(a,x,lo=0,hi=len(a))a中找x最左边数的索引,如果找不到就返回插入的索引.

``bisect.bisect(a, x, lo=0, hi=len(a))`找最右边的!

思路3:

分而治之,参考链接:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/discuss/14707/9-11-lines-O(log-n)

当然上面的时间复杂度都是:\(O(logn)\)


关注我的知乎专栏,了解更多的解题技巧,大家共同进步!

代码:

思路1:

版本1python

class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        if not nums:
            return [-1, -1]
        res = [-1, -1]
        left = 0
        n = len(nums)
        right = n - 1
        while left <= right:
            mid = left + (right - left) // 2
            if nums[mid] == target:
                left = mid
                right = mid
                while left > 0 and nums[left-1] == nums[left]:
                    left -= 1
                while right < n - 1 and nums[right] == nums[right+1]:
                    right += 1
                res[0] = left
                res[1] = right
                break
            elif nums[mid] < target:
                left = mid + 1
            else:
                right = mid - 1
        return res

版本1java

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] res = {-1, -1};
        int n = nums.length;
        if (n == 0) return res;
        int left = 0;
        int right = n-1;
        while (left <= right){
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                left = mid;
                right = mid;
                while (left > 0 && nums[left] == nums[left-1]) left--;
                while (right < n - 1 && nums[right] == nums[right+1]) right++;
                res[0] = left;
                res[1] = right;
                return res;
            }
            else if (nums[mid] > target) right = mid - 1;
            else left = mid + 1;
        }
        return res;
        
    }
}

思路1

版本2python

class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        if not nums:
            return [-1, -1]
        res = [-1, -1]
        left = 0
        n = len(nums)
        right = n
        while left < right:
            mid = left + (right - left) // 2
            if nums[mid] < target:
                left = mid + 1
            else:
                right = mid
        res[0] = left if left < n and nums[left] == target else -1
        if res[0] == -1:
            return res
        while left < n - 1 and nums[left] == nums[left + 1]:
            left += 1
        return res

版本2java

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] res = {-1, -1};
        int n = nums.length;
        if (n == 0) return res;
        int left = 0;
        int right = n;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        res[0] = (left < n && nums[left] == target) ? left : -1;
        if (res[0] == -1) return res;
        while (left < n - 1 && nums[left] == nums[left + 1]) left++;
        res[1] = left;
        return res;
        
    }
}

对库函数使用,大家自行操作的!

posted on 2019-05-07 16:37  威行天下  阅读(1013)  评论(0编辑  收藏  举报

导航