关注
写的挺工整的。但是有冗余部分,可以合并一下 import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { String s = scan.nextLine(); s = s.replace("[", "("); s = s.replace("{", "("); s = s.replace("]", ")"); s = s.replace("}", ")"); int res = solution(s); System.out.println(res); } } public static int solution(String s) { char ops = '+'; Stack<integer> stack = new Stack<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ' ') continue; if (c == '+' || c == '-' || c == '*' || c == '/') { ops = c; continue; } int num = 0; if (Character.isDigit(c)) { while (i < s.length() && Character.isDigit(s.charAt(i))) { num = num * 10 + s.charAt(i) - '0'; i++; } i--; } else if (c == '(') { int left = i; int right = i + 1; int count = 1; while (right < s.length() && count > 0) { if (s.charAt(right) == '(') count++; else if (s.charAt(right) == ')') count--; right++; } i = right - 1; num = solution(s.substring(left + 1, right - 1)); } if (ops == '+') stack.push(num); else if (ops == '-') stack.push(-num); else if (ops == '*') stack.push(stack.pop() * num); else if (ops == '/') stack.push(stack.pop() / num); } int res = 0; while (!stack.isEmpty()) res += stack.pop(); return res; } }</integer>
点赞
相关推荐
投递华为等公司10个岗位 >
点赞 评论 收藏
转发
点赞 评论 收藏
转发
03-21 09:44
Белорусский государственный университет 计算机类 点赞 评论 收藏
转发
点赞 评论 收藏
转发
牛客热帖
正在热议
# 牛客帮帮团来啦!有问必答 #
694814次浏览 11372人参与
# 你的秋招进展怎么样了 #
497681次浏览 13393人参与
# 找工作时遇到的神仙HR #
175969次浏览 1735人参与
# 如何写一份好简历 #
257074次浏览 3891人参与
# 许愿池 #
76634次浏览 1537人参与
# 找工作,你会甘心进小厂还是猛冲大厂 #
34393次浏览 350人参与
# 通信硬件人笔面经互助 #
105430次浏览 2141人参与
# 无实习如何秋招上岸 #
221737次浏览 3478人参与
# 互联网公司评价 #
78358次浏览 1078人参与
# 投了多少份简历才上岸 #
56152次浏览 945人参与
# 市场营销面经 #
4240次浏览 120人参与
# 产品人求职现状 #
50267次浏览 742人参与
# 面试中的破防瞬间 #
81793次浏览 1009人参与
# 软件开发投递记录 #
466550次浏览 7125人参与
# 如何缓解入职前的焦虑 #
33105次浏览 339人参与
# 互联网公司爆料 #
36100次浏览 367人参与
# 科大讯飞求职进展汇总 #
36234次浏览 369人参与
# 你会选择考研还是直接就业 #
74932次浏览 861人参与
# 2022届毕业生现状 #
316266次浏览 4398人参与
# 我的上岸简历长这样 #
162006次浏览 3248人参与