JavaScript no split no reverse, vallina


  • 0
    Z
    var s = '', b = new Array(0),i = 0, tmp = ''; str = ' '+str+' ';
    for(i=1;i<str.length-1;i++){
        if(str[i-1]==' '&&str[i]!=' ') tmp = str[i];
        if(str[i+1]==' '&&str[i]!=' ') b.push((str[i-1]==' ')?tmp:tmp+str[i]);
        if(str[i-1]!=' '&&str[i]!=' '&&str[i+1]!=' ') tmp += str[i];
    }
    for (i = b.length-1;i>=0;i--)
    {
        if (b[i]!=='') s += b[i]+' ';
    }
    var flag = true; i = s.length-1;
    while(flag===true&&i>=0){
        if(s.charAt(i)==' ') s =s.substr(0,i);
        else flag = false;
        i--;
    }
    return s;

Log in to reply
 

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