C# 102ms beats 100%


  • 0

    Every integer increases a branch and every '#' decrease a branch, and except the last '#', there should be always at least one branch left. And threre may be integers more than one digits, so just check the one before ','.

    
    for(int i = 1; i < n; i++){
        if(preorder[i] == ','){
            char pre = preorder[i-1];
            if(pre=='#')p--;
            else p++; //input is assured to be valid
            if(p<1)return false;
        }
    }
    
    return preorder[n-1]=='#'&&p==1;
    

Log in to reply
 

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