【leetcode】150. 逆波兰表达式求值

 

int evalRPN(char ** tokens, int tokensSize){
    int stack[5000], i, top=0;
    for (i=0; i<tokensSize; i++){
        if ( isdigit(tokens[i][0]) || strlen(tokens[i])>1 ){            
            stack[top++]=atoi(tokens[i]);
        }
        else{
            if (tokens[i][0] == '+')
                stack[top-2]=stack[top-2]+stack[top-1];
            else if(tokens[i][0] == '-')
                stack[top-2]=stack[top-2]-stack[top-1];
            else if(tokens[i][0] == '*')
                stack[top-2]=stack[top-2]*stack[top-1];
            else if(tokens[i][0] == '/')
                stack[top-2]=stack[top-2]/stack[top-1];
            top--;
        }
    }
    return stack[top-1];
}

 

posted @ 2020-12-24 14:40  温暖了寂寞  阅读(80)  评论(0编辑  收藏  举报