Number of Atoms——LeetCode进阶路
原题链接https://leetcode.com/problems/number-of-atoms/
题目描述
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.
思路分析
把给定化学式的原子数目分别统计出来,并返回原子和对应数目组成的字符串。
类似之前做的计算器,都是在于括号的嵌套,可以通过递归或者栈,其实两者思想上差不多啦
下面贴的题解是小陌看了花花酱大神之后的优化版,好多新特性自己还没有去使用的意识,还是用之前本方法,差距呐ヽ(。>д<)p
here are花花酱大神http://zxi.mytechroad.com/blog/string/leetcode-726-number-of-atoms/
AC解
class Solution {
private int i;
public String countOfAtoms(String formula) {
i = 0;
StringBuilder res = new StringBuilder();
Map<String,Integer> map = parse(formula);
for(String s:map.keySet())
{
res.append(s);
int number = map.get(s);
if(number > 1)
{
res.append("" + number);
}
}
return res.toString();
}
public Map<String,Integer> parse(String f)
{
Map<String,Integer> tm = new TreeMap();
while( i<f.length() && f.charAt(i) != ')')
{
if(f.charAt(i) == '(')
{
i ++;
for(Map.Entry<String,Integer> tra : parse(f).entrySet())
{
tm.put(tra.getKey(),tm.getOrDefault(tra.getKey(),0) + tra.getValue());
}
}
else
{
int begin = i ++;
while(i<f.length() && Character.isLowerCase(f.charAt(i)))
{
i ++;
}
String atom = f.substring(begin,i);
begin = i;
while(i<f.length() && Character.isDigit(f.charAt(i)))
{
i ++;
}
int count = begin < i ? Integer.parseInt(f.substring(begin,i)) : 1;
tm.put(atom,tm.getOrDefault(atom,0) + count);
}
}
int begin = ++ i;
while(i<f.length() && Character.isDigit(f.charAt(i)))
{
i ++;
}
if(begin < i)
{
int count = Integer.parseInt(f.substring(begin,i));
for(String s:tm.keySet())
{
tm.put(s,tm.get(s)*count);
}
}
return tm;
}
}
Number of Atoms——LeetCode进阶路的更多相关文章
- 【LeetCode】726. Number of Atoms 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/number-o ...
- [LeetCode] Number of Atoms 原子的个数
Given a chemical formula (given as a string), return the count of each atom. An atomic element alway ...
- LeetCode Number of Atoms
原题链接在这里:https://leetcode.com/problems/number-of-atoms/description/ 题目: Given a chemical formula (giv ...
- 【leetcode】726. Number of Atoms
题目如下: 解题思路:我用的是递归的方法,每次找出与第一个')'匹配的'('计算atom的数量后去除括号,只到分子式中没有括号为止.例如 "K4(ON(SO3)2)2" -> ...
- [Swift]LeetCode726. 原子的数量 | Number of Atoms
Given a chemical formula (given as a string), return the count of each atom. An atomic element alway ...
- Number of Islands——LeetCode
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 阿里巴巴 web前端性能优化进阶路
Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...
- LeetCode.874-走路机器人模拟(Walking Robot Simulation)
这是悦乐书的第335次更新,第360篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第205题(顺位题号是874).网格上的机器人从点(0,0)开始并朝北.机器人可以接收三 ...
- 瑞柏匡丞谈中国移动app的国际进阶路
当今3.0互联时代,已然形成了一个移动化,互动化,全球化的完整体系.瑞柏匡丞也在常年与国内外客户的交流沟通中有了自己的些许见解. 国内的移动产业的发展已然非常迅速,但也正是因为各类企业的不断崛起,能够 ...
- App开发革命进阶路
APP开发的成败,或许不是程序编辑的多么完美,界面设计的多么精致,其实这就是一场用户体验的革命. APP的使用场景是移动设备,人机交互以单指手“触点”为主,大段文字输入是交互的禁区.因此,APP带来了 ...
随机推荐
- 奥特曼autMan对接QQ机器人框架go-cqhttp的详细教程
前言 node-onebo用pm2,screen守护都不稳定,node-onebot扫码后ctrl+c,pm2没守护直接掉了,screen只能守护几个小时.因为服务器地区与你常登q的位置不同,时常有密 ...
- C++最基本调用动态链接库dll方法的小结
针对当时初学动态链接.静态链接,有些文档整理一下发出来算是给自己和读者一个小结. 首先创建DLL 编辑头文件 dllmain.h 头文件: #pragma once #if defined(_DLL_ ...
- Processing多窗口程序范例(三)
再来一例~另一种叠加方式呈现...看图: 程序 主程序: package syf.demo.multiwindow3; import processing.core.PApplet; import p ...
- python以及java环境搭建+解决不同版本环境共存问题
1.搭建python环境 1.安装python3.9.7,选择自定义安装.将文件添加至环境路径,然后选择下一步. 2.默认.然后选择下一步. 3.选择适用于所有使用者,选择自己想要存放的路径,然后选择 ...
- AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
引言 人工智能(AI)技术的迅猛发展推动了各行各业的数字化转型.图像分类,作为计算机视觉领域的核心技术之一,能够让机器自动识别图像中的物体.场景或特征,已广泛应用于医疗诊断.安防监控.自动驾驶和电子商 ...
- SecureCRT配置跳板机
跳板机(Jump Server),也称堡垒机,是一类可作为跳板批量操作远程设备的网络设备,是运系统管理员或运维人员常用的操作平台之一. 大家知道,在日常的开发中,有可能我们的本机不能够直接连线上的服务 ...
- SpringBoot+使用过滤器链执行风控决策
风控流程 下单前进行风控校验 // 1.begin---风控处理---前置处理{黑白名单校验} RiskControlRuleEnum controlRuleEnum = riskControlHan ...
- Docker 1.12 :认识 Swarm 模式下的节点崩溃处理
Posted on 2016年7月25日 上周小编为大家推荐了<Docker 1.12:用 Swarm 模式创建 Swarm 集群>,本周我们将深入为大家解读 1.12 版本 Docker ...
- 一个专业DBA应具备的技能
本文可以作为MySQL DBA面试官,以及候选人的双向参考 面试流程 接下来先说下我以往在做MySQL DBA面试时的过程(套路): 1.先自我介绍后,再让候选人花2-5分钟做下自我简介有不少人可能对 ...
- BUUCTF---rot
题目 破解下面的密文: 83 89 78 84 45 86 96 45 115 121 110 116 136 132 132 132 108 128 117 118 134 110 123 111 ...