6ms Simple Java Solution without Using Binary Search


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

  • 0
    M

    @XYLoo This is also what I did. Not sure why everyone else was so quick to use a binary search.

    E: I am so stupid. O(logn) is faster than linear XD


Log in to reply
 

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