【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:/ ...
随机推荐
- css浮动现象及清除浮动的方法
css浮动现象及清除浮动的方法 首先先明确浮动最初的定义及使用场景:实现文本环绕图片的效果. 除了用浮动外,目前暂无其他方法实现文本环绕 再来看看浮动的具体定义: 浮动的框可以左右移动,直至它 ...
- xiugai-去除js注释
<div class="myLoading"> <div class="svg-wrap"> <svg width="8 ...
- 多行文本溢出隐藏处理,兼容ie,火狐
问题 多行文本溢出隐藏,webkit内核浏览器如谷歌支持如下写法: overflow: hidden; text-overflow: ellipsis; display: -webkit-box; - ...
- MySQL部分索引
部分索引 char/varchar2太长,全部做索引的话,效率低,浪费存储空间 select avg(length(username)) from 索引统计: show index from tabl ...
- docker 保存镜像 加载镜像
1.保存镜像 docker save -o 保存的文件名 来源镜像 2.加载镜像 docker load -i 保存的文件名
- python之环境变量(测试环境可配置)
想要实现的结果是: 执行脚本时,带一个参数,由这个参数来决定测试环境(开发or测试),比如: python test.py dev 实现代码: 方式1 不用__getitem__方式: import ...
- Python——GUI可视化
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class ...
- Drone 持续集成实践 - 基于 Gogs,以 Golang 为例
Drone 官方示例 - Example Go project 用 Docker 部署 Go 服务器 Golang 官方示例 - outyet 一个生产环境的例子 用 rsync 复制文件的方式进行部 ...
- poj3614Sunscreen
Description To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her ...
- Web控件中Eval()的使用
1.使用Eval()绑定数据时使用三元运算符 <%#Eval("hg_A").ToString()=="1"?"通过":Eval(&q ...