# LWC 58：726. Number of Atoms

## LWC 58：726. Number of Atoms

Problem:

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.

```H2O2(SO2)23(OH)23

lf = "H2O2"
mid = "(SO2)23"
rt = "(OH)23"

```    public String countOfAtoms(String formula) {
Map<String, Integer> map = go(formula.toCharArray(), 0, formula.length() - 1);

StringBuilder sb = new StringBuilder();
List<String> set = new ArrayList<>(map.keySet());
Collections.sort(set);
for (String key : set) {
sb.append(key);
if (map.get(key) > 1) {
sb.append(map.get(key));
}
}
return sb.toString();
}

public Map<String, Integer> go(char[] cs, int s, int e){
if (s > e) return new HashMap<>();

Map<String, Integer> ans = new HashMap<>();
int idx = new String(cs).substring(s, e + 1).indexOf("(");
if (idx != -1) idx += s;
if (idx == -1) {

StringBuilder sb = new StringBuilder();
for (int i = s; i <= e;) {
if (Character.isUpperCase(cs[i])) {
sb.append(cs[i]);
i ++;

if (i <= e && Character.isUpperCase(cs[i])) {
String ele = sb.toString();
if (!ans.containsKey(ele)) {
ans.put(ele, 1);
}
else {
ans.put(ele, ans.get(ele) + 1);
}
sb = new StringBuilder();
}
else if (i <= e && Character.isLowerCase(cs[i])) {
while (i <= e && Character.isLowerCase(cs[i])) {
sb.append(cs[i]);
i ++;
}
if (i <= e && !Character.isDigit(cs[i])){
String ele = sb.toString();
if (!ans.containsKey(ele)) {
ans.put(ele, 1);
}
else {
ans.put(ele, ans.get(ele) + 1);
}
sb = new StringBuilder();
}
}

if (i <= e && Character.isDigit(cs[i])) {
int num = 0;
while (i <= e && Character.isDigit(cs[i])) {
num = num * 10 + cs[i] - '0';
i ++;
}

String ele = sb.toString();
if (!ans.containsKey(ele)) {
ans.put(ele, num);
}
else {
ans.put(ele, ans.get(ele) + num);
}

sb = new StringBuilder();
}
}
}

String ele = sb.toString();
if (!ele.isEmpty()) {
if (!ans.containsKey(ele)) {
ans.put(ele, 1);
}
else {
ans.put(ele, ans.get(ele) + 1);
}
}
return ans;
}
else {
Map<String, Integer> lf = go(cs, s, idx - 1);

StringBuilder sb = new StringBuilder();
int p = 1;
Map<String, Integer> include;
int num = 0;
int j = -1;
for (int i = idx + 1; i <= e; ++i) {
sb.append(cs[i]);
if (cs[i] == '(') {
p ++;
}
else if (cs[i] == ')') {
p --;
if (p == 0) {
j = i + 1;
while (j <= e && Character.isDigit(cs[j])) {
num = num * 10 + cs[j] - '0';
j ++;
}
break;
}
}
}
String ele = sb.toString().substring(0, sb.length() - 1);
include = go(ele.toCharArray(), 0, ele.length() - 1);

for (String key : include.keySet()) {
include.put(key, include.get(key) * num);
}

Map<String, Integer> rt = go(cs, j, e);

return ans;
}
}

public void add(Map<String, Integer> ans, Map<String, Integer> tmp) {
for (String key : tmp.keySet()) {
if (!ans.containsKey(key)) {
ans.put(key, tmp.get(key));
}
else {
ans.put(key, ans.get(key) + tmp.get(key));
}
}
}```

```    public String countOfAtoms(String formula) {
Map<String, Integer> map = go(formula.toCharArray(), 0, formula.length() - 1);

StringBuilder sb = new StringBuilder();
List<String> set = new ArrayList<>(map.keySet());
Collections.sort(set);
for (String key : set) {
sb.append(key);
if (map.get(key) > 1) {
sb.append(map.get(key));
}
}
return sb.toString();
}

public Map<String, Integer> go(char[] cs, int s, int e){
if (s > e) return new HashMap<>();

Map<String, Integer> ans = new HashMap<>();
int idx = new String(cs).substring(s, e + 1).indexOf("(");
if (idx != -1) idx += s;
if (idx == -1) {

StringBuilder sb = new StringBuilder();
for (int i = s; i <= e;) {
if (Character.isUpperCase(cs[i])) {
sb.append(cs[i]);
i ++;

while (i <= e && Character.isLowerCase(cs[i])){
sb.append(cs[i]);
i ++;
}

if (i <= e && !Character.isDigit(cs[i])){
String ele = sb.toString();
if (!ans.containsKey(ele)) {
ans.put(ele, 1);
}
else {
ans.put(ele, ans.get(ele) + 1);
}
sb = new StringBuilder();
}
else if (i <= e && Character.isDigit(cs[i])) {

int num = 0;
while (i <= e && Character.isDigit(cs[i])) {
num = num * 10 + cs[i] - '0';
i ++;
}

String ele = sb.toString();
if (!ans.containsKey(ele)) {
ans.put(ele, num);
}
else {
ans.put(ele, ans.get(ele) + num);
}

sb = new StringBuilder();
}
}
}

String ele = sb.toString();
if (!ele.isEmpty()) {
if (!ans.containsKey(ele)) {
ans.put(ele, 1);
}
else {
ans.put(ele, ans.get(ele) + 1);
}
}
return ans;
}
else {
Map<String, Integer> lf = go(cs, s, idx - 1);

StringBuilder sb = new StringBuilder();
int p = 1;
Map<String, Integer> include;
int num = 0;
int j = -1;
for (int i = idx + 1; i <= e; ++i) {
sb.append(cs[i]);
if (cs[i] == '(') {
p ++;
}
else if (cs[i] == ')') {
p --;
if (p == 0) {
j = i + 1;
while (j <= e && Character.isDigit(cs[j])) {
num = num * 10 + cs[j] - '0';
j ++;
}
break;
}
}
}
String ele = sb.toString().substring(0, sb.length() - 1);
include = go(ele.toCharArray(), 0, ele.length() - 1);

for (String key : include.keySet()) {
include.put(key, include.get(key) * num);
}

Map<String, Integer> rt = go(cs, j, e);

return ans;
}
}

public void add(Map<String, Integer> ans, Map<String, Integer> tmp) {
for (String key : tmp.keySet()) {
if (!ans.containsKey(key)) {
ans.put(key, tmp.get(key));
}
else {
ans.put(key, ans.get(key) + tmp.get(key));
}
}
}     ```

0 条评论

## 相关文章

1033

2929

### Map数据结构以及方法和数据遍历

前面说过Set和Map是ES6中的新的数据结构(不是数据类型是存储数据的集合结构),上面说过,Set类似与数据的形式而这个类似与object(对象),看...

3013

### 剑指offer第五天

28.数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半，请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2...

2755

1142

22810

2557

7939

### Q7 Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 1...

2964

812