Skip to main content

35. Search Insert Position

·68 words·1 min· loading
Table of Contents

problem

IDEA
#

  • We can use Binary Search to solve the problem with O(logn)O(\log n) time complexity.

CODE
#

class Solution {
    public int searchInsert(int[] nums, int target) {
        int l = 0, r = nums.length - 1, mid = 0;
        
        while (l<=r){
            mid = (l+r) /2 ;

            if (nums[mid] < target){
                l  = mid + 1;
            }else{
                r = mid -1;
            }
        }

        return l;
        
    }
}