zoukankan      html  css  js  c++  java
  • Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation.

    Valid operators are +-*/. Each operand may be an integer or another expression.

    Some examples:

      ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
      ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
    

    使用堆来做,遇到数字插入stack,遇到符号则pop两个元素进行运算,然后把结果push进stack。 注意先pop出来的是第二个数字。

     1 public class Solution {
     2     public int evalRPN(String[] tokens) {
     3         Stack<Integer> st = new Stack<Integer>();
     4         for(int i = 0; i < tokens.length; i ++){
     5             if(tokens[i].equals("+")){
     6                 int second = Integer.valueOf(st.pop());
     7                 int first = Integer.valueOf(st.pop());
     8                 st.push(first + second);
     9             }else if(tokens[i].equals("-")){
    10                 int second = Integer.valueOf(st.pop());
    11                 int first = Integer.valueOf(st.pop());
    12                 st.push(first - second);               
    13             }else if(tokens[i].equals("*")){
    14                 int second = Integer.valueOf(st.pop());
    15                 int first = Integer.valueOf(st.pop());
    16                 st.push(first * second);  
    17             }else if(tokens[i].equals("/")){
    18                 int second = Integer.valueOf(st.pop());
    19                 int first = Integer.valueOf(st.pop());
    20                 st.push((Integer)(first / second));  
    21             }else{
    22                 st.push(Integer.valueOf(tokens[i]));
    23             }
    24         }
    25         return st.pop();
    26     }
    27 }

     第二遍:

     1 public class Solution {
     2     public int evalRPN(String[] tokens) {
     3         Stack<Integer> stack = new Stack<Integer>();
     4         for(int i = 0; i < tokens.length; i ++){
     5             if(tokens[i].equals("+")){
     6                 stack.push(stack.pop() + stack.pop());
     7             }else if(tokens[i].equals("-")){
     8                 int a = stack.pop();
     9                 stack.push(stack.pop() - a);
    10             }else if(tokens[i].equals("*")){
    11                 stack.push(stack.pop() * stack.pop());
    12             }else if(tokens[i].equals("/")){
    13                 int a = stack.pop();
    14                 stack.push((Integer)(stack.pop() / a));
    15             }else
    16                 stack.push(Integer.valueOf(tokens[i]));
    17         }
    18         return stack.pop();
    19     }
    20 }
  • 相关阅读:
    一个误操作,导致mysql所有表打不开,我是不是应该删库跑路?非著名喷子
    SqlServer 一些跟时间相关的东西
    C# 在代码里调用其他Webapi
    Typroa + smms-uploader 实现上传图片到 SM.MS 图床
    Redis哨兵模式
    给 Git 仓库瘦身,删除大文件的版本控制
    修复 UEditor 上传视频的相关问题
    MongoDB 海量数据高效读写
    .NET 5中 Autofac 的使用
    Dapper 的 AspNetCore 扩展包
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3455740.html
Copyright © 2011-2022 走看看