6ms Java solution


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

Log in to reply
 

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