JavaScript. some thing strange.


  • 0
    R

    i find there are some thing strange.
    when i add a useless variable cur and do cur ++ in for loop, i just spend 180 ms. And when i remove this useless variable and operation, i spend more time about 250 ms.
    Is there some thing wrong with leetcode?
    ...
    var lengthOfLongestSubstring = function(s) {
    let res = 0, cur = 0, str = [];

    str[0] = s[0];
    
    const slen = s.length;
    for(let i = 0; i < slen; i ++){
        let repeat = false;
        
        const strlen = str.length;
        for(let j = 0; j < strlen; j ++){
            if(s[i] === str[j]){
                repeat = true;
                break;
            }
        }
        
        if(repeat){
            str = str.slice(str.indexOf(s[i]) + 1);
        }
        
        str.push(s[i]);
        res = res > str.length ? res : str.length;
        
        cur ++;
    }
    
    return res;
    

    };
    ...


Log in to reply
 

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