Time Limit Exceed for javascript solution


  • 1
    L

    I got time limit exceed error for the following solution:

    var TwoSum = function() {
        this.num = {};
    };
    
    TwoSum.prototype.add = function(input) {
        var count = (input in this.num) ? this.num[input] : 0; 
        this.num[input] = count+1;
    };
    
    TwoSum.prototype.find = function(val) {
        var num = this.num;
        for(var key in num) {
            var x = val - key;
            if (x == key) {
                if(num[key] == 2) {
                    return true;
                } 
            } 
            else if(x in num) {
                return true;   
            }
        }
        return false;
        
    };
    

    Anyone can help to improve the solution ?


  • 0
    K

    I think this is just due to language performance


  • 0
    D

    @lindan_xmu similar problem here!


Log in to reply
 

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