My Accepted Java solution

  • -1
    public class Solution {
    public int searchInsert(int[] A, int target) {
        int len = A.length;
            return 0;
        for(int i=0; i<len-1; i++){
                return i+1;
        return len;


  • 0

    Binary search will do better here with O(log n). Also, in your solution, you don't have to check both current and next element in the array since it's already sorted. Check the current element, and if it's greater than or equal to target, then its place needs to be given to target.

Log in to reply

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