12
返回列表 发新帖
楼主: kunge12345
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 【求教】basic calculator之疯狂follow up

🔗
twjeric 2018-3-9 15:48:44 | 只看该作者
全局:
自己写的follow up 3,不一定完全对:
  1. public class Main {
  2.     private static HashMap<Character, Integer> priority = new HashMap();
  3.    
  4.     public static void main(String[] args) {
  5.         priority.put('+', 1); priority.put('-', 1);
  6.         priority.put('*', 2); priority.put('/', 2);
  7.         priority.put('^', 3);
  8.         priority.put('=', 0);
  9.         String expr = "3 + 5 * 4 ^ (2 - 1) * 10";
  10.         System.out.println(calc(expr));
  11.     }
  12.    
  13.     public static int calc(String expr) {
  14.         Deque<Integer> vals = new ArrayDeque();
  15.         Deque<Character> ops = new ArrayDeque();
  16.         expr += "=";
  17.         char[] array = expr.toCharArray();
  18.         int num = 0;
  19.         for (int idx = 0; idx < array.length; idx++) {
  20.             char c = array[idx];
  21.             if (c == ' ') continue;
  22.             if (c == '(') {
  23.                 int start = idx;
  24.                 int cnt = 1;
  25.                 while (cnt != 0) {
  26.                     char tmp = array[++idx];
  27.                     if (tmp == '(') cnt++;
  28.                     if (tmp == ')') cnt--;
  29.                 }
  30.                 num = calc(expr.substring(start + 1, idx));
  31.             }
  32.             else if (Character.isDigit(c)) {
  33.                 num = num * 10 + (c - '0');
  34.             }
  35.             else {
  36.                 while (!ops.isEmpty() &&
  37.                        priority.get(ops.peek()) >= priority.get(c)) {
  38.                     char op = ops.pop();
  39.                     if (op == '+') {
  40.                         num += vals.pop();
  41.                     }
  42.                     else if (op == '-') {
  43.                         num = vals.pop() - num;
  44.                     }
  45.                     else if (op == '*') {
  46.                         num *= vals.pop();
  47.                     }
  48.                     else if (op == '/') {
  49.                         num = vals.pop() / num;
  50.                     }
  51.                     else if (op == '^') {
  52.                         num = (int) Math.pow(vals.pop(), num);
  53.                     }
  54.                 }
  55.                 vals.push(num);
  56.                 ops.push(c);
  57.                 num = 0;
  58.             }
  59.         }
  60.         return vals.pop();
  61.     }
  62. }
复制代码
回复

使用道具 举报

全局:
stellari 发表于 2017-1-9 07:34
先考虑无括号的情况:
1. 每次读一个数字Ni和一个操作符Pi,因为暂时不能断定是否Pi现在就能够运算,所以 ...

谢谢!这个思路很清楚!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表