034 Find First and Last Position of Element in Sorted Array

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].

Example:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

解释下题目:

给定一个有序的数组,给定一个数字,找出这个数字在这个有序数组中第一次出现和最后一次出现的位置

1. 二分法查找

实际耗时:4ms

public int[] searchRange(int[] nums, int target) {
        int mid = binarySearch(nums, target);
        int[] result = {-1, -1};
        if (-1 == mid) {
            return result;
        } else {
            int left = mid;
            int right = mid;
            result[0] = left;
            result[1] = right;
            while (left >= 1) {
                if (nums[left] == nums[left - 1]) {
                    left--;
                } else {
                    result[0] = left;
                    break;
                }
                //这句至关重要
                result[0] = left;
            }
            while (right < nums.length - 1) {
                if (nums[right] == nums[right + 1]) {
                    right++;
                } else {
                    result[1] = right;
                    break;
                }
                //这句至关重要
                result[1] = right;
            }
            return result;
        }

    }

    /**
     * 二分查找法
     *
     * @param nums   待查找的数组
     * @param target 目标数字
     * @return 对应的下标,不存在返回-1
     */
    public static int binarySearch(int[] nums, int target) {
        int small = 0;
        int big = nums.length - 1;
        int mid;
        int result = -1;
        while (big >= small) {
            mid = (big + small) >> 1;
            if (nums[mid] == target) {
                result = mid;
                return result;
            } else if (nums[mid] > target) {
                big = mid - 1;
            } else {
                small = mid + 1;
            }
        }
        return result;
    }
踩过的坑:{1,1} target = 1 ; {2,2} target = 2

  思路:首先使用二分法找到对应数字的下标,然后开始往前找和往后找。稍微注意下注释中的“至关重要”语句。

时间复杂度O(log n)
空间复杂度O(1)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,040评论 0 10
  • <center>#1 Two Sum</center> link Description:Given an arr...
    铛铛铛clark阅读 2,426评论 0 3
  • 回想往事,酸甜苦辣皆有!我始终追寻着自己的方向。总想找个肩膀依靠。找到了吗。开始我觉得找到了。可是走着走着...
    绽放自我阅读 285评论 0 2
  • 1. 定义 迭代器模式:提供一种方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节。 迭代器是为容器服务...
    小楠总阅读 480评论 0 0
  • 堂号:一指清代科举,童生经县、府、院三级考试合格成秀才,府考时取中前十名者被列为堂号;二指旧时一户人家的家族称号。...
    薛崔愿阅读 5,712评论 1 6

友情链接更多精彩内容