why my JS solution TLE


  • 1
    A
    var findDisappearedNumbers = function(nums) {
        var result = [];
        var len = nums.length;
        for(var i = 1; i<=len; i++){
            var index = nums.indexOf(i);
            if(index === -1) {
                result.push(i);
            }else{
                nums.splice(index,1);
            }
        }
        return result;
    };
    

  • 0
    J

    I think, "indexOf" function was causing the problem.
    The time complexity of indexOf is O(n).
    So the time complexity of your solution is at least O(n^2).


Log in to reply
 

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