A quick and simple Java solution


  • 0
    W
    public class Solution {
        public int searchInsert(int[] A, int target) {
            int index;
    		for(index=0;index<A.length;++index){
    			if(A[index]>=target){
    				return index;
    			}
    		}
    		return index;
        }
    }

  • 0
    Y

    The worse case time complexity is O(N).
    Using binary search is better. It's O(logN)


Log in to reply
 

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