Created
February 2, 2018 05:48
-
-
Save jianminchen/853271cbe0945f2fc930e646cb343e9d to your computer and use it in GitHub Desktop.
Leetcode 726 study Java code first, and then write a C# solution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source code from http://zxi.mytechroad.com/blog/string/leetcode-726-number-of-atoms/ | |
// Author: Huahua | |
// Runtime: 13 ms | |
// Feb. 1, 2018 plan to study the code and write a C# solution | |
class Solution { | |
private int i; | |
public String countOfAtoms(String formula) { | |
StringBuilder ans = new StringBuilder(); | |
i = 0; | |
Map<String, Integer> counts = countOfAtoms(formula.toCharArray()); | |
for (String name: counts.keySet()) { | |
ans.append(name); | |
int count = counts.get(name); | |
if (count > 1) ans.append("" + count); | |
} | |
return ans.toString(); | |
} | |
private Map<String, Integer> countOfAtoms(char[] f) { | |
Map<String, Integer> ans = new TreeMap<String, Integer>(); | |
while (i != f.length) { | |
if (f[i] == '(') { | |
++i; | |
Map<String, Integer> tmp = countOfAtoms(f); | |
int count = getCount(f); | |
for (Map.Entry<String, Integer> entry : tmp.entrySet()) | |
ans.put(entry.getKey(), | |
ans.getOrDefault(entry.getKey(), 0) | |
+ entry.getValue() * count); | |
} else if (f[i] == ')') { | |
++i; | |
return ans; | |
} else { | |
String name = getName(f); | |
ans.put(name, ans.getOrDefault(name, 0) + getCount(f)); | |
} | |
} | |
return ans; | |
} | |
private String getName(char[] f) { | |
String name = "" + f[i++]; | |
while (i < f.length && 'a' <= f[i] && f[i] <= 'z') name += f[i++]; | |
return name; | |
} | |
private int getCount(char[] f) { | |
int count = 0; | |
while (i < f.length && '0' <= f[i] && f[i] <= '9') { | |
count = count * 10 + (f[i] - '0'); | |
++i; | |
} | |
return count == 0 ? 1 : count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment