my java solution

  • 0

    slower but easy to write

    public int searchInsert(int[] nums, int target) {
            int l = nums.length;
            for(int i = 0; i <= l-1; i++){
                if(nums[i] == target) return i;
                else if(nums[0] > target) return 0;
                else if( nums[i]> target) return i;
            return l;

    num[0] == target can be put outside the for loop

Log in to reply

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