Home LeetCode. 35. Search Insert Position
Post
Cancel

LeetCode. 35. Search Insert Position

image

[Link] https://leetcode.com/problems/search-insert-position/


1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
    public int searchInsert(int[] nums, int target) {
        int l = 0, r = nums.length - 1, mid;
        while(l <= r){
            mid = (l + r)/2;
            if(nums[mid] > target) r = mid - 1;
            else if(nums[mid] == target) return mid;
            else l = mid + 1;
        }
        return l;
    }
}
This post is licensed under CC BY 4.0 by the author.