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带来了 ...
随机推荐
- Jenkins - [01] 概述
"持续集成并不能消除Bug,而是让它们非常容易发现和改正." -- Martin Fowler 一.概述 1.1.持续集成(CI) 持续集成(Continuous integr ...
- mac强制关闭程序
使用快捷键:Command+Option+Esc 来打开"强制退出应用程序"的窗口,然后选中你需要退出的程序,再点右下方的"强制退出"即可.
- docker pause 命令使用
暂停正在运行的镜像容器 用途是在启动的容器的过程又的容器启动快了 有的还没有就绪 调试过程使用 a3: 正在运行的镜像容器简称 暂停: docker pause a3 解除暂停: docker unp ...
- Netty源码—7.ByteBuf原理二
大纲 9.Netty的内存规格 10.缓存数据结构 11.命中缓存的分配流程 12.Netty里有关内存分配的重要概念 13.Page级别的内存分配 14.SubPage级别的内存分配 15.Byte ...
- mosquitto的基本参数使用
当使用mosquitto_pub命令时,可以使用多个参数来配置发布行为.以下是mosquitto_pub命令的一些常见参数及其详细说明: -h, --host <hostname>:指定连 ...
- 【Ubuntu】安装Eclipse
[Ubuntu]安装Eclipse 零.前言 Eclipse是一个使用很广泛的代码编辑软件,最近换了Ubuntu系统,需要安装一下,故记录. 壹.下载 Eclipse到官网下载,或者到镜像源下载,官网 ...
- 【虚拟机】VirualBox安装macOS系统
[虚拟机]VirualBox安装macOS系统 零.创建虚拟机 类型选择 Mac OS X 版本选择 macOS 10.13 High Sierra (64-bit) 注意:这边我设置的名称为 Mac ...
- 【大数据】HBase 集群部署:全流程详细步骤解析
[大数据]HBase 集群部署:全流程详细步骤解析 前言 本文帮助你从零搭建一个三台机器(虚拟机)的HBase集群,适用于大数据专业或者用到HBase的同学查看,由于操作步骤过多,特此记录,完整操作过 ...
- javascript 数值交换技巧
需求 现在存在var a = 1,b = 2;,要求使用javascript实现a & b数值的交换,让a = 2,b = 1. 中间变量(常规实现方式) var a = 1, b = 2; ...
- 实现Andriod的APP中文名
让程序编译后,就会自动生成中文名,以及启动界面. 1. 2.AndroidManifest.xml的修改. 如果新建project,自动生成,否则要手动 3. 4.ok了.到手机端看结果吧