- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址: https://leetcode.com/problems/number-of-atoms/description/
Given a chemical formula
(given as a string), return the count of each atom.
Anatomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.
1or 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.
Twoformulas concatenated together produce another formula. For example, H2O2He3Mg4 is also a formula.
Aformula 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:
给出了一个化学分式,计算里面的原子个数是多少,并且按照原子字母的递增有序输出。
这个题第一眼就看到了括号,立马想到了括号匹配问题。括号匹配问题使用一个记数指针,遇到左括号加一,遇到右括号减一,如果该记数指针等于0了,说明找到了匹配的括号。在这个题中就相当于找到了一个分子团,该分子团后面会有个数字,代表这个分子团出现的次数。所以,做法就是如果不是分子团,那么统计元素的个数;如果是分子团,那么把这个分子团当做分子,计算里面元素的个数再乘以外边的分子团的个数。所以就是个DFS问题。
比较难办的就是寻找每个元素,需要根据大小写和数字等判断;寻找个数,需要把字符串转成10进制。最后把分子式内的元素个数×分子式的个数的时候,按照元素迭代的方式做,不要使用对分子式个数的for循环去累加。
最坏的时间复杂度是O(N!),最优时间复杂度是O(N),空间复杂度是O(N)。其中N是分子的长度。
class Solution(object):
def countOfAtoms(self, formula):
"""
:type formula: str
:rtype: str
"""
count = self.dfs(formula)
res = ""
for atom, num in sorted(count.items()):
if num == 1:
res += atom
else:
res += atom + str(num)
return res
def dfs(self, formula):
count = collections.Counter()
if not formula: return count
i = 0
while i < len(formula):
if formula[i].isalpha(): 首字母是英文字符
atom = formula[i]
atomNum = 0
找到这个元素所有字符
i += 1
while i < len(formula) and formula[i].isalpha() and formula[i].islower():
atom += formula[i]
i += 1
while i < len(formula) and formula[i].isdigit(): 后面是否有数字
atomNum = 10 * atomNum + int(formula[i])
i += 1
count[atom] += 1 if atomNum == 0 else atomNum # 使用加号
elif formula[i] == "(": 括号匹配
left = i 左括号位置
parent = 1 统计括号个数
while i < len(formula) and parent != 0:
i += 1
if formula[i] == "(":
parent += 1
elif formula[i] == ")":
parent -= 1
right = i
atomNum = 0
i += 1
while i < len(formula) and formula[i].isdigit(): 后面是否有数字
atomNum = 10 * atomNum + int(formula[i])
i += 1
innerCount = self.dfs(formula[left + 1 : right])
for c, n in innerCount.items():
count[c] += n * atomNum
count += self.dfs(formula[i + 1 :])
return count
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
看到括号匹配,也会让人立马想到栈,其实DFS本身就是栈实现的,所以也完全可以用栈来解决。
方法是,左括号进栈,然后把字母依次进栈,当遇到右括号的时候,需要对栈进行退栈操作,这个时候要统计每个元素的次数,当退栈的时候遇到左括号,说明内部的分子团已经结束,那么把遇到的第一个左括号退栈,把内部的分子团的各个元素和其个数进栈。然后遍历就好了!这个方法的好处是,当最后遍历结束的时候,栈里面保存的只剩下了已经统计好了的各个元素和其个数的对应,每个元素只会出现一次,相当于已经做了元素的求和操作,最后只需要排序即可。
为了方便,我把分子式用括号包了起来,方便栈操作的判断。
这个题做了很久,主要是查一个bug,查了一个小时,感觉很诡异。其实仔细对比一下和上面DFS的解法,大同小异。区别是我用字母n保存了分子式的长度,然后下面退栈的for循环中又使用了n这个变量名称!!由于python不用声明变量,所以直接把外边的n覆盖掉了!!做法很简单,把内部for循环里的变量名改一下就好了!生气!!
最坏的时间复杂度是O(N!),最优时间复杂度是O(N),空间复杂度是O(N)。其中N是分子的长度。
代码如下:
class Solution(object):
def countOfAtoms(self, formula):
"""
:type formula: str
:rtype: str
"""
stack = list()
formula = "(" + formula + ")1"
i = 0
n = len(formula)
while i < n:
if i >= n: continue
if formula[i] == "(":
stack.append("(")
i += 1
elif formula[i] == ")":
parentNum = 0
i += 1
while i < n and formula[i].isdigit():
parentNum = 10 * parentNum + int(formula[i])
i += 1
count = collections.Counter()
while stack[-1] != "(":
atom, atomNum = stack.pop()
count[atom] += atomNum * parentNum
if stack[-1] == "(":
stack.pop()
for c, t in count.items(): 刚开始把变量t写成了n!!错了很多次
stack.append((c, t))
elif formula[i].isalpha():
atom = formula[i]
atomNum = 0
i += 1
while i < n and formula[i].isalpha() and formula[i].islower():
atom += formula[i]
i += 1
while i < n and formula[i].isdigit():
atomNum = 10 * atomNum + int(formula[i])
i += 1
atomNum = 1 if atomNum == 0 else atomNum
stack.append((atom, atomNum))
res = ""
for atoms in sorted(stack):
if atoms == "(":
continue
c, n = atoms
if n == 1:
res += c
else:
res += c + str(n)
return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
从 angular 5.1 更新到 6.1 后,我开始从我的代码中收到一些错误,如下所示: Error: ngc compilation failed: components/forms/utils.
我正在学习 Typescript 并尝试了解类型和接口(interface)的最佳实践。我正在玩一个使用 GPS 坐标的示例,想知道一种方法是否比另一种更好。 let gps1 : number[];
type padding = [number, number, number, number] interface IPaddingProps { defaultValue?: padding
这两种格式在内存中保存结果的顺序上有什么区别吗? number = number + 10; number += 10; 我记得一种格式会立即保存结果,因此下一行代码可以使用新值,而对于另一种格式,
在 Python 匹配模式中,如何匹配像 1 这样的文字数字在按数字反向引用后 \1 ? 我尝试了 \g用于此目的的替换模式中可用的语法,但它在我的匹配模式中不起作用。 我有一个更大的问题,我想使用一
我的源文件here包含 HTML 代码,我想将电话号码更改为可在我的应用程序中单击。我正在寻找一个正则表达式来转换字符串 >numbernumber(\d+)$1numbernumber<",我们在S
我们有一个包含 2 个字段和一个按钮的表单。我们想要点击按钮来输出位于 int A 和 int B 之间的随机整数(比如 3、5 或 33)? (不需要使用 jQuery 或类似的东西) 最佳答案 你
我收到以下类型错误(TypeScript - 3.7.5)。 error TS2345: Argument of type '(priority1: number, priority2: number
只想创建简单的填充器以在其他功能中使用它: function fillLine(row, column, length, bgcolor) { var sheet = SpreadsheetApp
我有一个问题。当我保存程序输出的 *.txt 时,我得到以下信息:0.021111111111111112a118d0 以及更多的东西。 问题是: 这个数字中的“d0”和“a”是什么意思? 我不知道“
首先:抱歉标题太长了,但我发现很难用一句话来解释这个问题;)。是的,我也四处搜索(这里和谷歌),但找不到合适的答案。 所以,问题是这样的: 数字 1-15 将像这样放在金字塔中(由数组表示):
我想从字符串中提取血压。数据可能如下所示: text <- c("at 10.00 seated 132/69", "99/49", "176/109", "10.12 I 128/51, II 1
当尝试执行一个简单的 bash 脚本以将前面带有 0 的数字递增 1 时,原始数字被错误地解释。 #!/bin/bash number=0026 echo $number echo $((number
我有一个类型为 [number, number] 的字段,TypeScript 编译器(strict 设置为 true)出现问题,提示初始值值(value)。我尝试了以下方法: public shee
你能帮我表达数组吗:["232","2323","233"] 我试试这个:/^\[("\d{1,7}")|(,"\d{1,7}")\]$/ 但是这个表达式不能正常工作。 我使用 ruby(rail
这个问题在这里已经有了答案: meaning of (number) & (-number) (4 个回答) 关闭6年前. 例如: int get(int i) { int res = 0;
我正在考虑使用 Berkeley DB作为高度并发的移动应用程序后端的一部分。对于我的应用程序,使用 Queue对于他们的记录级别锁定将是理想的。但是,如标题中所述,我需要查询和更新概念建模的数据,如
我正在尝试解决涉及重复数字的特定 JavaScript 练习,为此我需要将重复数字处理到大量小数位。 目前我正在使用: function divide(numerator, denominator){
我有这个数组类型: interface Details { Name: string; URL: string; Year: number; } interface AppState {
我们正在使用 Spring 3.x.x 和 Quartz 2.x.x 实现 Web 应用程序。 Web 服务器是 Tomcat 7.x.x。我们有 3 台服务器。 Quartz 是集群式的,因此所有这
我是一名优秀的程序员,十分优秀!