5.二分查找--实现有重复数字的升序数组的

题目描述

请实现有重复数字的升序数组的二分查找

给定一个 元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1

示例1

输入

复制

[1,2,4,4,5],4

返回值

复制

2

说明

从左到右,查找到第1个为4的,下标为2,返回2 
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 如果目标值存在返回下标,否则返回 -1
     * @param nums int整型一维数组 
     * @param target int整型 
     * @return int整型
     */
    public int search (int[] nums, int target) {
        // write code here
        int index=-1;
        int low=0;
        int high=nums.length-1;
        while(low<=high){
        int  mid=(high+low)/2;
        if(target==nums[mid])
        {
            index=mid;
            high=mid-1;
        }else if (target>nums[mid]){
            low=mid+1;
        }else if(target<nums[mid])
        {
            high=mid-1;
        }
            }
        return  index;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40408443/article/details/116090877