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

    String equals() ==

     1 public class Solution {
     2     public int evalRPN(String[] tokens) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5         Stack<Integer> stack = new Stack<Integer>();
     6         if(tokens == null || tokens.length == 0)
     7             return 0;
     8         for(int i = 0; i < tokens.length; i++){
     9             
    10             String str = tokens[i];
    11             if(str.equals("+")){
    12                 int num1 = stack.pop();
    13                 int num2 = stack.pop();
    14                 stack.push(num1 + num2);
    15             }
    16             else if(str.equals("-")){
    17                 int num1 = stack.pop();
    18                 int num2 = stack.pop();
    19                 stack.push(num2 - num1);
    20             }
    21             else if(str.equals("*")){
    22                 int num1 = stack.pop();
    23                 int num2 = stack.pop();
    24                 stack.push(num1 * num2);
    25             }
    26             else if(str.equals("/")){
    27                 int num1 = stack.pop();
    28                 int num2 = stack.pop();
    29                 stack.push(num2 / num1);
    30             }
    31             else
    32                 stack.push(Integer.valueOf(str));
    33         }
    34         return stack.pop();
    35     }
    36 }
  • 相关阅读:
    最长上升子序列(实验回顾)
    数据库应用开发一、vs
    全文检索
    mangtomant 增删改查
    django
    SQLAlchemy 增删改查 一对多 多对多
    Flask-Sqlalchemy—常用字段类型说明
    flask
    文件下载
    python连接mongodb
  • 原文地址:https://www.cnblogs.com/jasonC/p/3446917.html
Copyright © 2011-2022 走看看