Javascript using Map() Time Limit Exceeded


  • 2
    E
    var TwoSum = function() {
        this.tSum = new Map();
    };
    
    /**
     * @param {number} input
     * @returns {void}
     */
    TwoSum.prototype.add = function(input) {
        if(this.tSum.has(input) === false){
            this.tSum.set(input,1);
        }
        else{
            this.tSum.set(input, this.tSum.get(input) + 1);
        }
    };
    
    /**
     * @param {number} val
     * @returns {boolean}
     */
    TwoSum.prototype.find = function(val) {
        this.tSum.forEach(function(value,key,map){
            map.set(key,value-1);
            var left = val - key;
            if(map.has(left) && map.get(left) >0){
                return true;
            }
        });
        return false;
    };

Log in to reply
 

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