Java O(n) time complexity


  • 0
    I

    '''
    class Solution {
    public int searchInsert(int[] nums, int target) {
    int counter = 0;
    boolean b = false;
    for (int i = 0; i < nums.length; i++){
    if (nums[i] == target){
    b = true;
    counter = i;
    }
    }
    if (b == false){
    for (int i = 0; i < nums.length; i++){
    if (target > nums[i]){
    counter++;
    }
    }
    }
    return counter;
    }
    }
    '''


Log in to reply
 

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