6 Lines of code, Java


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

Log in to reply
 

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