Javascript method


  • 1
    I
    var lengthOfLongestSubstring = function(s) {
        var a = s.split('');
        var result = [];
        var num = 1;
        var flag = true;
        var rCount;
        if (a.length === 0){
            return 0;
        }
        for (var i = 0; i < a.length; i ++) {
            for (rCount = 0; rCount < result.length; rCount++) {
                if (result[rCount] === a[i] ) {
                    flag = false;
                    if(num < result.length) {
                        num = result.length;
                    }
                    break;
                } else {
                    flag = true;
                }
    
            }
            if (!flag) {
                result.splice(0,rCount+1);
            }
            result.push(a[i]);
        }
        if (num < result.length) {
            num = result.length;
        }
        return num;
    };
    

Log in to reply
 

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