zoukankan      html  css  js  c++  java
  • [LeetCode] 726. 原子的数量

    大模拟。。。烦这种题
    遇到括号匹配的题目,用栈就对了。此题需要栈+Map
    没啥难度,写了半天,Debug几个用例后,1A过了

    class Solution {
        class Atom {
            String s;
            int cnt = 1;
    
            Atom(String s, int cnt) {
                this.s = s;
                if (cnt == 0) cnt = 1;
                this.cnt = cnt;
            }
        }
    
        public String countOfAtoms(String formula) {
            Stack<Atom> stack = new Stack<>();
            Map<String, Atom> map = new HashMap<>();
            int n = formula.length();
    
            Atom b = new Atom("(", 0);
    
            String tmp = "";
            int cnt = 0;
            for (int i = 0; i < n; i++) {
                char c = formula.charAt(i);
                if (c >= 'a' && c <= 'z') {
                    tmp += c;
                    continue;
                }
    
                if (c >= '0' && c <= '9') {
                    cnt *= 10;
                    cnt += c - '0';
                    continue;
                }
    
                if (c >= 'A' && c <= 'Z') {
                    if ("".equals(tmp)) {
                        tmp += c;
                    } else {
                        if (tmp.length() > 0)
                            stack.push(new Atom(tmp, cnt));
                        tmp = "" + c;
                        cnt = 0;
                    }
                    continue;
                }
    
    
                if (c == '(') {
                    if (tmp.length() > 0)
                        stack.push(new Atom(tmp, cnt));
                    tmp = "";
                    cnt = 0;
                    stack.push(b);
                    continue;
                }
    
                if (c == ')') {
                    if (tmp.length() > 0)
                        stack.push(new Atom(tmp, cnt));
                    tmp = "";
                    cnt = 0;
    
                    i++;
                    while (i < n) {
                        c = formula.charAt(i);
                        if (c >= '0' && c <= '9') {
                            cnt *= 10;
                            cnt += c - '0';
                        } else {
                            break;
                        }
                        i++;
                    }
                    i--;
                    if (cnt == 0) cnt = 1;
    
                    List<Atom> list = new ArrayList<>();
                    while (true) {
                        Atom pop = stack.pop();
                        if (pop.s.equals("(")) break;
                        pop.cnt *= cnt;
                        list.add(pop);
                    }
                    stack.addAll(list);
                    cnt = 0;
                }
            }
            if (tmp.length() > 0)
                stack.push(new Atom(tmp, cnt));
    
            List<Atom> ans = new ArrayList<>(stack);
            for (Atom an : ans) {
                if (map.containsKey(an.s)) {
                    map.get(an.s).cnt += an.cnt;
                } else {
                    map.put(an.s, an);
                }
            }
            Stream<Atom> sorted = map.values().stream().sorted(Comparator.comparing(o -> o.s));
    
            StringBuilder ret = new StringBuilder();
            sorted.forEach(an->{
                ret.append(an.s);
                if (an.cnt > 1) ret.append(an.cnt);
            });
    
            return ret.toString();
        }
    }
    
  • 相关阅读:
    判断一个点是否在一个不规则多边形内算法
    vue-cli 3.0 安装和创建项目流程
    微信小程序分享朋友圈的实现思路与解决办法
    vue2.0中关于active-class
    Nginx服务启动脚本
    Linux系统优化
    URL检测脚本
    Mysql读写分离php脚本
    Memcahed服务异常监控脚本
    一致性哈希算法PHP测试片段
  • 原文地址:https://www.cnblogs.com/acbingo/p/14974729.html
Copyright © 2011-2022 走看看