【leetcode刷题笔记】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


题解:典型的栈的应用——计算后缀表达式。

思路很简单:

遇到符号就从栈里面弹出来两个元素进行相应的计算后得到的结果重新放回栈中

遇到数字就直接压入栈中。

代码如下:

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

特别注意的地方有两点:

1.将一个String转换成Integer用Integer.parseInt()函数

2.开始我用的是“==”来比较两个字符串是否相等,后来发现“==”其实比较的是字符串的地址是否相等,如果要比较字符串的内容是否相等要用s.equals()函数。

不过很奇怪的一点是在自己电脑的eclipse上面用“==”居然也能够算出正确的值。

【leetcode刷题笔记】Evaluate Reverse Polish Notation

时间: 2024-08-29 11:10:22

【leetcode刷题笔记】Evaluate Reverse Polish Notation的相关文章

LeetCode第一题:Evaluate Reverse Polish Notation

时间:2014.06.11 地点:基地 -------------------------------------------------------------------- 一.题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another express

【leetcode刷题笔记】Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

【leetcode刷题笔记】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

力扣算法题—150. 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. Note: Division between two integers should truncate toward zero. The given RPN expression

lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]

LeetCode OJ——练习笔记(1)Evaluate Reverse Polish Notation

院招终于开始了,然后期待与兴奋过后却是面临着笔试一次又一次的失败,然后开始留意到LeetCode. 也想自己去体验一下诸多大牛通向无限coding路上都攻克过的一关. 话不多说,贴出原题: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expres

[LeetCode][JavaScript]Evaluate Reverse Polish Notation

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", "+",

[leetcode]Evaluate Reverse Polish Notation @ Python

原题地址:https://oj.leetcode.com/problems/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

[LeetCode]Evaluate Reverse Polish Notation

题目:Evaluate Reverse Polish Notation 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", &quo