1

题目描述: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.给定一个排序好的数组和一个目标,找出目标在数组中的位置或者他应该在的位置

这道题目很简单。
可以采用二分查找法。

        int low = 0;
        int high = nums.length-1;
        
        while(low <= high){
            int mid = (low+high)/2;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] > target){
                high = mid -1;
            }else{
                low = mid +1;
            }
            
        }
        return low;

soleil阿璐
350 声望45 粉丝

stay real ~