leetCode 34.Search for a Range (搜索范围) 解题思路和方法

Search for a Range

Given a sorted array of integers, 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].

For example,

Given [5, 7, 7, 8, 8, 10] and target value 8,

return [3, 4].

思路:此题在思考的时候走了些弯路,一心想着一个循环解决问题,但是写代码的时候总是不能很好的解出。最后突然想起来,完全可以先二分查找最低的位置,然后再查找最高位置即可,这样就很简单了。不过里面还是有一些细节需要注意。

具体代码如下:

public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] ans  = new int[]{-1,-1};
        //排除特殊情况
        if(nums.length == 0 || nums[0] > target || nums[nums.length-1] < target)
            return ans;
        //首尾都相等
        if(nums[0]== target && nums[nums.length-1] == target){
        	ans[0] = 0;
        	ans[1] = nums.length - 1;
        	return ans;
        }
        //二分查找
        int low = 0;
        int hight = nums.length - 1;
        int mid = 0;
        //先求符合要求的起始值
        while(low <= hight){
        	mid = (low + hight)/2;
        	if(nums[mid] > target){
        		hight = mid -1;
        	}else if(nums[mid] < target){
        		low = mid + 1;
        	}else{
        		hight = mid;
        	}//判断结束情况
        	if(mid > 0 && nums[mid] == target && nums[mid -1] < target){
        		break;
        	}else if(mid == 0 && nums[mid] == target){
        		break;
        	}
        }
        //是否需要赋值。如果最低位置不存在,那么最高位置也不存在
        if(nums[mid] == target){
            ans[0] = mid;
            //再求符合要求的最大位置
            low = mid;//起始值设为target的最低位置
            hight = nums.length - 1;
            while(low <= hight){
            	mid = (low + hight)/2;
            	if(mid < nums.length - 1 && nums[mid + 1] == target){
            		mid ++;//这里很关键,因为(low+hight)/2自动向下取整的,所以看情况+1或向上取整
            	}
            	//分情况更新位置
            	if(nums[mid] > target){
            		hight = mid -1;
            	}else if(nums[mid] < target){
            		low = mid + 1;
            	}else{
            		low = mid;
            	}
            	//判断最高位置
            	if(mid <nums.length-1 && nums[mid] == target && nums[mid +1] > target){
            		break;
            	}else if(mid == nums.length-1 && nums[mid] == target){
            		break;
            	}
            }
            ans[1] = mid;//最低位存在,最高位肯定也存在
        }
        return ans;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 02:50:38

leetCode 34.Search for a Range (搜索范围) 解题思路和方法的相关文章

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(l

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 34. 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

LeetCode 34. Search for a Range (找到一个范围)

Given an array of integers 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]. For e

LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定数字的起止下标 采用两遍扫描: 第一遍扫描得到给定数字的起始下标,(从下标i==0开始到nums.lenght-1) 第二遍扫描从第一遍扫描得到的下标开始进行扫描 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 数组中找到targe

Leetcode 34 Search for a Range (二分搜索 lower_bound和upper_bound)

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order ofO(logn). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7

Java [leetcode 34]Search for a Range

题目描述: Given a sorted array of integers, 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]. For example, Given

LeetCode 34 Search for a Range (C,C++,Java,Python)

Problem: Given a sorted array of integers, 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]. For example, Giv

leetCode 79.Word Search (词搜索) 解题思路和方法

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board = [ ["ABCE"]