【leetcode】726. Number of Atoms
题目如下:
解题思路:我用的是递归的方法,每次找出与第一个')'匹配的'('计算atom的数量后去除括号,只到分子式中没有括号为止。例如 "K4(ON(SO3)2)2" -> "K4(ONS2O6)2" -> "K4O2N2S4O12"。接下来再对分子式进行分割,得出每个atom的数量后排序即可。原理很简单,代码写得很乱,仅供参考。
代码如下:
class Solution(object):
def recursive(self,formula):
left = right = None
for i,v in enumerate(formula):
if v == '(':
left = i
elif v == ')':
right = i
break
if left == None and right == None:
return formula
lf = formula[:left]
parse = formula[left+1:right]
times = ''
for i in range(right+1,len(formula)):
if formula[i].isdigit():
times += formula[i]
else:
if i != len(formula) - 1:
i -= 1
break if times != '':
times = int(times) rf = formula[i+1:] if times == '':
ts = parse
else:
parseList = []
val = ''
val_num = ''
parse += '#'
for i in parse:
#print parseList
if i.islower():
val += i
#parseList.append(val)
elif i.isupper():
if val != '':
parseList.append(val)
if val_num != '':
parseList.append(str(int(val_num) * int(times)))
val_num = ''
elif val_num == '' and val != '':
parseList.append(str(times))
val = i
elif i.isdigit():
if val != '':
parseList.append(val)
val = ''
val_num += i
elif i == '#':
if val != '':
parseList.append(val)
if val_num != '':
parseList.append(str(int(val_num) * int(times)))
elif val_num == '' and val != '':
parseList.append(str(times))
ts = ''.join(parseList)
return self.recursive(lf + ts + rf) def countOfAtoms(self, formula):
"""
:type formula: str
:rtype: str
"""
f = self.recursive(formula)
i = 1 #print f #transform MgO2H2 -> Mg1O2H2
while i < len(f):
if f[i].isupper() and f[i-1].isdigit() == False:
f = f[:i] + '' + f[i:]
i = 1
i += 1
if f[-1].isdigit() == False:
f += '' dic = {} key = ''
val = '' # H11He49N1O35B7N46Li20
for i in f:
if i.isdigit():
val += i
else:
if val == '':
key += i
else:
if key not in dic:
dic[key] = int(val)
else:
dic[key] += int(val)
key = i
val = '' if key not in dic:
dic[key] = int(val)
else:
dic[key] += int(val) keys = dic.keys()
keys.sort()
res = ''
#print dic
for i in keys:
res += i
if dic[i] > 1:
res += str(dic[i])
return res
【leetcode】726. Number of Atoms的更多相关文章
- 【LeetCode】726. Number of Atoms 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/number-o ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
- 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
随机推荐
- 提取的js,要先部署在远程,再引入
var meet = { _w: document.documentElement.clientWidth, _h: document.documentElement.clientWidth, ini ...
- HDU 3605 Escape(二分图多重匹配问题)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- Step3 - How to: Host and Run a Basic Windows Communication Foundation Service
This is the third of six tasks required to create a Windows Communication Foundation (WCF) applicati ...
- 线段树2(P3373)
传送 感谢洛谷题解让我理清了这一撮标记 这里多了一个乘法操作,乘法的优先级高于加法.我们来思考一下有关标记的问题. 首先由两种操作,可以想到要有两个标记,一个标记乘法(mul[k]),一个标记加法(a ...
- flutter输入颜色枚举卡顿假死
AndroidStudio 3.3.2 遇到 flutter输入颜色枚举卡顿假死,目前没好的解决方案,可以设置显示时间或者关闭popup窗口显示文档,这样就不会卡顿了 下面示例代码在输入 Colors ...
- brdd 惰性执行 mapreduce 提取指定类型值 WebUi 作业信息 全局临时视图 pyspark scala spark 安装
[rdd 惰性执行] 为了提高计算效率 spark 采用了哪些机制 1-rdd 基于分布式内存数据集进行运算 2-lazy evaluation :惰性执行,即rdd的变换操作并不是在运行该代码时立 ...
- git清空远程仓库
需求背景:因为用jenkins连接了git仓库,有时候job构建出现问题,需要排查问题,但是呢,真实的项目代码量非常pang大,所以就需要建1个测试仓库,使用最少量的代码能复现自己的问题就好. 这就需 ...
- Oralce-资源配置PROFILE
profile:作为用户配置文件,它是密码限制,资源限制的命名集合 在安装数据库时,Oracle自动会建立名为default的默认配置文件 使用profile文件时,要注意以下几点: 建立用户时,如果 ...
- Win7 64位注册32位DLL
记忆力越来越差,备忘. 参考地址 https://support.microsoft.com/en-us/help/249873/how-to-use-the-regsvr32-tool-and-tr ...
- [功能集锦] 002 - mysql查询数据库字典+导出+样式一键整合至excel
写在前面: 因为工作时候经常遇到半路接手项目的情况,由于年代久远,数据库字典这块经常缺失.故写此篇,以便复用,也希望对大家有点帮助. 随笔内容不高级,如有不妥,不吝指正. 20190730-加了一些简 ...