给定一个逆波兰表达式,求表达式的值。
数据范围:表达式长度满足
,表达式中仅包含数字和 + ,- , * , / ,其中数字的大小满足
。
import java.util.*;
/**
* NC216 逆波兰表达式求值
* @author d3y1
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tokens string字符串一维数组
* @return int整型
*/
public int evalRPN (String[] tokens) {
Stack<Integer> stack = new Stack<>();
int leftVal,rightVal;
for(String token: tokens){
switch(token){
case "+": rightVal=stack.pop();leftVal=stack.pop();stack.push(leftVal+rightVal); break;
case "-": rightVal=stack.pop();leftVal=stack.pop();stack.push(leftVal-rightVal); break;
case "*": rightVal=stack.pop();leftVal=stack.pop();stack.push(leftVal*rightVal); break;
case "/": rightVal=stack.pop();leftVal=stack.pop();stack.push(leftVal/rightVal); break;
default: stack.push(Integer.parseInt(token)); break;
}
}
// + - * /
// String regex = "[+\\-*/]";
// for(String token: tokens){
// if(token.matches(regex)){
// rightVal = stack.pop();
// leftVal = stack.pop();
// switch(token){
// case "+": stack.push(leftVal+rightVal); break;
// case "-": stack.push(leftVal-rightVal); break;
// case "*": stack.push(leftVal*rightVal); break;
// case "/": stack.push(leftVal/rightVal); break;
// default: break;
// }
// }else{
// stack.push(Integer.parseInt(token));
// }
// }
return stack.pop();
}
} public int evalRPN (String[] tokens) {
// write code here
Stack<Integer> s = new Stack<>();
int f, sd;
for (String str : tokens) {
if (str.equals("+")) {
s.push(s.pop() + s.pop());
} else if (str.equals("-")) {
f = s.pop();
sd = s.pop();
s.push(sd - f);
} else if (str.equals("*")) {
s.push(s.pop() * s.pop());
} else if (str.equals("/")) {
f = s.pop();
sd = s.pop();
s.push(sd / f);
} else {
s.push(Integer.parseInt(str));
}
}
return s.pop();
} 遇到运算符就运算 结果入栈。注意-和/的先后顺序就好/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param tokens string字符串一维数组
* @return int整型
*/
public static int evalRPN(String[] tokens) {
// write code here
Deque<String> stack = new ArrayDeque<>();
for (String str : tokens) {
if ("+".equals(str) || "-".equals(str) || "*".equals(str) || "/".equals(str)) {
Integer optionNum2 = Integer.parseInt(stack.pop());
Integer optionNum1 = Integer.parseInt(stack.pop());
switch (str) {
case "+":
stack.push(String.valueOf(optionNum1 + optionNum2));
break;
case "-":
stack.push(String.valueOf(optionNum1 - optionNum2));
break;
case "*":
stack.push(String.valueOf(optionNum1 * optionNum2));
break;
case "/":
stack.push(String.valueOf(optionNum1 / optionNum2));
}
} else {
stack.push(str);
}
}
return Integer.parseInt(stack.pop());
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tokens string字符串一维数组
* @return int整型
*/
public int evalRPN (String[] tokens) {
// write code here
Stack<Integer> stack = new Stack<>();
// int res= 0;
for(String token : tokens){
if(token.matches("-?[0-9]+")){
stack.push(Integer.valueOf(token));
}else{
Integer a = stack.pop();
Integer b = stack.pop();
int res = compute(b,a,token);
stack.push(res);
}
}
return stack.pop();
}
public static int compute(Integer a, Integer b, String token){
if(token.equals("+")){
return a+b;
} else if(token.equals("-")){
return a-b;
} else if(token.equals("*")){
return a*b;
}
return a/b;
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tokens string字符串一维数组
* @return int整型
*/
public int evalRPN (ArrayList tokens) {
// write code here
Stack<String> stack = new Stack<>();
for (int i = 0; i < tokens.size(); i++) {
if (judgeIsNum((String)tokens.get(i))) {
stack.push((String)tokens.get(i));
} else {
//取出栈顶的两个元素与当前运算符进行计算
int a = Integer.parseInt(stack.pop());
int b = Integer.parseInt(stack.pop());
int res = 0;
if ("+".equals((String)tokens.get(i)) )
res = a + b;
if ("-".equals((String)tokens.get(i)))
res = b - a;
if ("*".equals((String)tokens.get(i)))
res = a * b;
if ("/".equals((String)tokens.get(i)))
res = b / a;
//再将计算结果压入栈中
stack.push(String.valueOf(res));
}
}
return Integer.parseInt(stack.pop());
}
public boolean judgeIsNum(String s) {
try {
Integer.parseInt(s);
} catch (Exception e) {
return false;
}
return true;
}
} public class Solution {
public int evalRPN (String[] tokens) {
int a;
int b;
int result;
Stack stack = new Stack();
for(int i=0;i<tokens.length;i++) {
switch(tokens[i]) {
case "+":
a=stack.pop();
b=stack.pop();
result=b+a;
stack.push(result);
break;
case "-":
a=stack.pop();
b=stack.pop();
result=b-a;
stack.push(result);
break;
case "*":
a=stack.pop();
b=stack.pop();
result=b*a;
stack.push(result);
break;
case "/":
a=stack.pop();
b=stack.pop();
result=b/a;
stack.push(result);
break;
default:
stack.push(Integer.parseInt(tokens[i]));
}
}
return stack.top();
}
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tokens string字符串一维数组
* @return int整型
*/
public int evalRPN (String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < tokens.length; i++) {
if (!isOp(tokens[i])) {
stack.push(Integer.parseInt(tokens[i]));
} else {
int num2 = stack.pop();
int num1 = stack.pop();
stack.push(doOp(num1,tokens[i],num2));
}
}
return stack.pop();
}
private boolean isOp(String str) {
if ("+".equals(str) ||
"-".equals(str) ||
"*".equals(str) ||
"/".equals(str)) return true;
return false;
}
private int doOp(int num1,String op,int num2) {
if ("+".equals(op)) return num1+num2;
else if ("-".equals(op)) return num1-num2;
else if ("*".equals(op)) return num1*num2;
else return num1/num2;
}
} public int evalRPN (String[] tokens) {
ArrayDeque<Integer> stack = new ArrayDeque<>();
for (String t : tokens) {
if (t.equals("+") || t.equals("-")
|| t.equals("*") || t.equals("/")) {
int a = stack.pop(), b = stack.pop();
if (t.equals("+")) stack.push(b+a);
else if (t.equals("-")) stack.push(b-a);
else if (t.equals("*")) stack.push(b*a);
else stack.push(b/a);
} else {
stack.push(Integer.parseInt(t));
}
}
return stack.peek();
} import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param tokens string字符串一维数组
* @return int整型
*/
public int evalRPN (String[] tokens) {
// write code here
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < tokens.length; i++) {
if(isDigit(tokens[i])){
stack.push(Integer.parseInt(tokens[i]));
}else{
if(tokens[i].equals("+")){
int num2 = stack.pop();
int num1 = stack.pop();
stack.push(num1 + num2);
}else if(tokens[i].equals("-")){
int num2 = stack.pop();
int num1 = stack.pop();
stack.push(num1 - num2);
}else if(tokens[i].equals("*")){
int num2 = stack.pop();
int num1 = stack.pop();
stack.push(num1 * num2);
}else if(tokens[i].equals("/")){
int num2 = stack.pop();
int num1 = stack.pop();
stack.push(num1 / num2);
}
}
}
return stack.pop();
}
private boolean isDigit(String s) {
try{
Integer.parseInt(s);
return true;
}catch(Exception e) {
return false;
}
}
}