zoukankan      html  css  js  c++  java
  • LeetCode Number of Atoms

    原题链接在这里:https://leetcode.com/problems/number-of-atoms/description/

    题目:

    Given a chemical formula (given as a string), return the count of each atom.

    An atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.

    1 or more digits representing the count of that element may follow if the count is greater than 1. If the count is 1, no digits will follow. For example, H2O and H2O2 are possible, but H1O2 is impossible.

    Two formulas concatenated together produce another formula. For example, H2O2He3Mg4 is also a formula.

    A formula placed in parentheses, and a count (optionally added) is also a formula. For example, (H2O2) and (H2O2)3 are formulas.

    Given a formula, output the count of all elements as a string in the following form: the first name (in sorted order), followed by its count (if that count is more than 1), followed by the second name (in sorted order), followed by its count (if that count is more than 1), and so on.

    Example 1:

    Input: 
    formula = "H2O"
    Output: "H2O"
    Explanation: 
    The count of elements are {'H': 2, 'O': 1}.

    Example 2:

    Input: 
    formula = "Mg(OH)2"
    Output: "H2MgO2"
    Explanation: 
    The count of elements are {'H': 2, 'Mg': 1, 'O': 2}.

    Example 3:

    Input: 
    formula = "K4(ON(SO3)2)2"
    Output: "K4N2O14S4"
    Explanation: 
    The count of elements are {'K': 4, 'N': 2, 'O': 14, 'S': 4}.

    Note:

    • All atom names consist of lowercase letters, except for the first character which is uppercase.
    • The length of formula will be in the range [1, 1000].
    • formula will only consist of letters, digits, and round parentheses, and is a valid formula as defined in the problem.

    题解:

    每当遇到'(', 代表更深的一层开始, 遇到')'表示本层的结束.

    对于每一层用map来计数出现的元素和对应的count. 当一层结束,就把本层的map值加回到上层中. 

    保留上一层就用到了stack.

    Time Complexity: O(n). n = formula.length().

    Space: O(n).

    AC Java:

     1 class Solution {
     2     public String countOfAtoms(String formula) {
     3         if(formula == null || formula.length() == 0){
     4             return formula;
     5         }
     6         
     7         int len = formula.length();
     8         
     9         // stack里存的是本层元素和对应的count
    10         Stack<Map<String, Integer>> stk = new Stack<Map<String, Integer>>();
    11         stk.push(new TreeMap<String, Integer>());
    12         
    13         int i = 0;
    14         while(i<len){
    15             if(formula.charAt(i) == '('){
    16                 // 遇到'('就说明到了新的一层,需要放新的stack
    17                 stk.push(new TreeMap<String, Integer>());
    18                 i++;
    19             }else if(formula.charAt(i) == ')'){
    20                 // 遇到')'说明本层结束, 找到乘数, 计算结果加回到上层stack中
    21                 Map<String, Integer> top = stk.pop();
    22                 i++;
    23                 int iStart = i;
    24                 while(i<len && Character.isDigit(formula.charAt(i))){
    25                     i++;
    26                 }
    27                 
    28                 int multi = 1;
    29                 if(i > iStart){
    30                     multi = Integer.valueOf(formula.substring(iStart, i));
    31                 }
    32                 
    33                 Map<String, Integer> currentTop = stk.peek();
    34                 for(String s : top.keySet()){
    35                     int value = top.get(s);
    36                     currentTop.put(s, currentTop.getOrDefault(s, 0) + value*multi);
    37                 }
    38             }else{
    39                 int iStart = i++;
    40                 while(i<len && Character.isLowerCase(formula.charAt(i))){
    41                     i++;
    42                 }
    43                 
    44                 String name = formula.substring(iStart, i);
    45                 
    46                 iStart = i;
    47                 while(i<len && Character.isDigit(formula.charAt(i))){
    48                     i++;
    49                 }
    50                 int multi = i>iStart ? Integer.valueOf(formula.substring(iStart, i)) : 1;
    51                 Map<String, Integer> currentTop = stk.peek();
    52                 currentTop.put(name, currentTop.getOrDefault(name, 0) + multi);
    53             }
    54         }
    55         
    56         StringBuilder sb = new StringBuilder();
    57         Map<String, Integer> currentTop = stk.peek();
    58         for(String name : currentTop.keySet()){
    59             sb.append(name);
    60             int value = currentTop.get(name);
    61             if(value > 1){
    62                 sb.append(value);
    63             }
    64         }
    65         
    66         return sb.toString();
    67     }
    68 }
  • 相关阅读:
    ubuntu 安装redis以及phpredis
    【译】关于Rust模块的清晰解释
    【译】Ringbahn的两个内存Bug
    从背单词到写代码
    【译】Rust中的array、vector和slice
    【译】理解Rust中的闭包
    【译】Rust,无畏并发
    Linux环境下发布.net core
    负载均衡之nginx
    mysql数据库变更监控(canal)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/8267525.html
Copyright © 2011-2022 走看看