Java solution with O(n) in worst case


  • 0
    A
    class Solution {
        public int searchInsert(int[] nums, int target) {
            int count = 0;
            while(count < nums.length && nums[count] <= target) {
                if (nums[count] == target) {
                    return count;
                }
                count++;
            }
            return count;
        }
    }
    

Log in to reply
 

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