Java solution


  • 0
    R
     public int searchInsert(int[] nums, int target) {
            
            // first element
            if ((target == nums[0]) || (target < nums[0]) ) return 0;
          
            
            int temp = nums[0];
            int i = 1;
            for(i = 1; i<nums.length; i++) {
               if (target == nums[i]) return i;
               else if (target > temp && target < nums[i]) return i;
               else temp = nums[i];
            }
            
            return i;
        }
    

Log in to reply
 

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