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();
        }
    }
    
  • 相关阅读:
    JS 中 this 关键字详解
    Excel 文本函数
    Excel 日期和时间函数
    Excel引用和数学函数
    Excel-查找函数
    Excel-统计函数
    数据分析-业务知识
    Excel-逻辑函数
    Excel-基本操作
    电商数据分析总结
  • 原文地址:https://www.cnblogs.com/acbingo/p/14974729.html
Copyright © 2011-2022 走看看