My straight forward 10 lines Java solution


  • -3
    F

    The execution time may not be perfect but it's easy to read and understand, I think...

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

    }


  • 0
    N

    when the N is big enough, it's O(N), not suitable though easy to read and understand


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.