逆波兰(Reverse Polish Notation)

类似这样的后缀表达式: 叫做逆波兰表达式["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (5 / 13)) -> 4

编译器比较喜欢从栈(stack)里面 pop 两个对象出来计算 然后 继续push 回去, 然后继续这样子计算下去..算法也很简单
     public static void main(String[] args) {
		System.out.println(calRPN("4", "13", "5", "/", "+"));
	}

	public static int calRPN(String... tokens) {
		int ret = 0;
		String operators = "+-*/";
		Stack<String> stack = new Stack<String>();
		for (String t : tokens) {
			if (!operators.contains(t)) {
				stack.push(t);
			}else {
				int a = Integer.valueOf(stack.pop());
				int b = Integer.valueOf(stack.pop());
				int v = 0;

				if (t.equals("+")) {
					v = a + b;
				}else if (t.equals("-")) {
					v = a - b;
				}else if (t.equals("*")) {
					v = a * b;
				}else if (t.equals("/")) {
					v = a / b;
				}
				stack.push(String.valueOf(v));
			}
		}
		ret = Integer.valueOf(stack.pop());
		return ret;
	}
时间: 2024-10-16 00:33:10

逆波兰(Reverse Polish Notation)的相关文章

利用逆波兰(Reverse Polish Notation, RPN)的后缀表达法计算四则运算表达式

如果是单纯的加减运算表达式,非常简单,依次处理表达式的头字符就可以了. 但是对于四则运算来说,有括号,又有先乘除,后加减使得表达式处理变得负责. 20世纪50年代,波兰逻辑学家Jan Lukasiewicz发明了不需要括号的后缀表达式,精妙地解决的这个问题. 比如说 char sInput[]="9+(3-1)*3+8/2"; //output 931-3*+82/+ 经过转换成 931-3×+82/+,然后从头开始遍历每个字符,当遇到运算符的时候,就把该运算符的前两个数进行操作,比如

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

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

[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", "*"] -> (

【LeetCode-面试算法经典-Java实现】【151-Evaluate Reverse Polish Notation(计算逆波兰式)】

[151-Evaluate Reverse Polish Notation(计算逆波兰式)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some

【LeetCode刷题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", "*"] -&g

Java Evaluate Reverse Polish Notation(逆波兰表达式)

表达式:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9   ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 题目大意:给定一个逆波兰表达式,求该表达式的值 思路:由于逆波兰表达式本身

[Swift]LeetCode150. 逆波兰表达式求值 | 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

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. Some examples: ["2", "1", "+", "3", "*"] -&g

leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation

leetcode 逆波兰式求解 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", "