My Java Solution, easy to understand


  • 0
    L

    public class Solution {
    public int searchInsert(int[] nums, int target) {
    int n = 0;

        while(n <= nums.length-1 && nums[n] < target){
            n++;
            
        }
        
        return n;
    }
    

    }


  • 0
    public int searchInsert(int[] nums, int target) {
    int d=nums.length;
    for(int i=0;i<nums.length;i++)
    {
      
        if(target<=nums[i])
        {d=i;
         break;
        }
        
    }
    
    return d;
    
    
        
    }
    

    }


  • 1
    J

    my code same with you.
    the array has been sorted...


Log in to reply
 

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