Straight forward java solution


  • 0
    V

    public class Solution {

    public int searchInsert(int[] A, int target) {
    
        if(A.length == 0){
            return 0;
        }
        
        if(A.length == 1){
            if(target > A[0]){
                return 1;
            }else{
                return 0;
            }
        }
        
        if(A[0] > target){
            return 0;
        }
        
        for(int i = 0; i < A.length - 1; i++){
            
            if(A[i] == target){
                return i;
            }
            
            if(A[i+1] == target){
                return i+1;
            }
            
            if(A[i] < target && target < A[i+1]){
                return i + 1;
            }
        }
        
        if(A[A.length - 1] < target){
            return A.length;
        }
        
        return 0;
    }
    

    }


Log in to reply
 

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