【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:/ ...
随机推荐
- 导航栏图标切换:click事件,hover事件
最近再做一个基于angular6的项目,导航栏需求:1.hover切换图标 2.click切换图标 先用jquery实现功能,在在angular组件里面实现. demo如下: <!DOCTYPE ...
- selenium设定请求头
如果只是需要设定User-Agent可以用如下所示方式 dcap = dict(DesiredCapabilities.PHANTOMJS)dcap["phantomjs.page.sett ...
- hibernate搭建及其增删改查
一.jar包 最基础的hibernatejar包,以及数据库驱动的jar包 二.数据库 t_user表 id int 主键 自动增长 name varchar() 三.配置文件 <?xml ve ...
- go tour - Go 入门实验教程
在线实验地址 - 官网 在线实验地址 - 国内 可以将官方教程作为独立程序在本地安装使用,这样无需访问互联网就能运行,且速度更快,因为是在你的机器上构建并运行代码示例. 本地运行此教程的中文版的步骤如 ...
- js面向对象程序设计之构造函数
再上一篇的开头说了创建对象的两种方式,一种是Object构造函数的方式,一种是对象字面量的方法.但这些方式创建多个对象的时候都会产生大量的重复代码.经过技术的进步也演化出来许多的创建对象的模式.本章会 ...
- JNI-java native interface(java本地接口)
什么是JNI java native interface(java本地接口) ABI: application binary interface (应用程序二进制接口) 为什么要使用JNI * 复用很 ...
- Python算法每日一题--001--给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...
- mooc-IDEA alter enter--008
十四.IntelliJ IDEA -alter enter 1.提示一:自动创建函数 2.提示二.list replace 等于 3.提示三.字符串formate或build 等于 等于 等于 4.实 ...
- 什么是php工厂模式
工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式.著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见.今天我们就为大家介绍一下PHP中的 ...
- php页面出现空白解决方法
查询php程序使用内存情况 $size = memory_get_usage(); file_put_contents("d:/data.php", var_export($siz ...