题解 | #牛的表达式计算器#

牛的表达式计算器

https://www.nowcoder.com/practice/261e7f01438f414c92f59c0059d3a906

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param tokens string字符串一维数组 
     * @return int整型
     */
    public int calculatePostfix (String[] tokens) {
        // write code here
        Stack<Integer> st = new Stack<>();
        int fir;
        int sec;
        for(int i = 0;i < tokens.length;i++){
            switch (tokens[i]){
                case "+":
                    st.add(st.pop() + st.pop());
                    break;
                 case "-":
                    fir = st.pop();
                    sec = st.pop();
                    st.add(sec - fir);
                    break;
                 case "*":
                    st.add(st.pop() * st.pop());
                    break;
                 case "/":
                    fir = st.pop();
                    sec = st.pop();
                    st.add(sec / fir);
                    break;
                default:
                    st.add(Integer.parseInt(tokens[i]));
            }
        }
        return st.pop();
    }
}

全部评论

相关推荐

04-27 15:01
早稲田大学 Java
牛客72191338...:可能是时间点的问题,四月底机会确实会相对少点,但佬这个学历摆在这,会有机会的
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务