AC import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { ArrayList<Integer> inputs = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); if(line != null && ! line.isEmpty()) { int res = resolve(line.trim()); System.out.println(String.valueOf(res)); } } // write your code here public static int resolve(String expr) { Stack<Integer> stack = new Stack<>(); String[] split = expr.split("\\s+"); for (int i = 0; i < split.length; i ++) { if(split[i].equals("^")) { if(stack.size() < 1) return - 1; stack.push(stack.pop() + 1); } else if(split[i].equals("+")) { if(stack.size() < 2) return - 1; stack.push(stack.pop() + stack.pop()); } else if(split[i].equals("*")) { if(stack.size() < 2) return - 1; stack.push(stack.pop() * stack.pop()); } else { if(stack.size() > 16) return - 2; stack.push(Integer.parseInt(split[i])); } } return stack.pop(); } }
点赞 评论

相关推荐

10-15 10:23
门头沟学院 Java
牛可乐的头像真牛:赶紧举报,这公司绝对是诈骗的,等你签约后工作一两个月后根据合同漏洞把你开除,并且要求你赔偿3w培训费,996是为了提前筛选心甘情愿签下合同容易受骗的群体,纯粹面向校招生精心设计的骗局
你见过哪些工贼行为
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务