LeetCode-35:搜索插入位置(Java语言实现)

class Solution {
    public int searchInsert(int[] nums, int target) {
        int i = 0, j = nums.length;
        int mid;
        while (i + 1 < j) {
            mid = (i + j) >>> 1;
            if (nums[mid] < target) {
                i = mid;
            } else {
                j = mid;
            }
        }
        if (target == nums[i] || target < nums[i]) {
            return i;
        } else {
            return i + 1;
        }
    }
}

你可能感兴趣的:(leetcode,java,算法,排序算法,数据结构,笔记)