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();
        }
    }
    
  • 相关阅读:
    Red5/FMS视频直播带宽计算
    基于NPOI开源框架写的ExcelHelper
    Using C# 4.0 and dynamic to parse JSON
    跟我学MVVM模式开发
    supermap使用代码示例(GIS)
    使用OpenXML将Excel内容读取到DataTable中
    ADO 数据类型转换表
    I don't like Regex...
    将Datatable转Excel少于4笔时汉字乱码4/26
    记录宝宝成长脚印3/31
  • 原文地址:https://www.cnblogs.com/acbingo/p/14974729.html
Copyright © 2011-2022 走看看