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带来了 ...
随机推荐
- Python - [01] 简介
人生苦短,我用Python 一.Python 是什么 Python是一个高层次的结合了解释型.编译型.互动性和面向对象的脚本语言. Python的设计具有很强的可读性,相比其他语言经常使用英文关键字, ...
- Easyexcel(3-文件导出)
响应头设置 通过设置文件导出的响应头,可以自定义文件导出的名字信息等 //编码格式为UTF-8 response.setCharacterEncoding("UTF-8"); // ...
- 使用 Trae 辅助开发一个网页文本高亮插件
项目概述 本插件是一个用于在网页上高亮显示选中文本的工具,支持多种颜色选择.高亮复制和删除等功能.插件基于 Edge 扩展测试开发. 目前只大致开发了内容交互脚本和插件弹出页,后台管理页暂未开发. 项 ...
- python 打开yaml文件提示Empty test suite.
网上方案: 我自己: 将test改为其他名称开头即可 读取成功
- golang 使用goto进行多错误处理
goto 语句介绍 在 Go 语言中,可以通过goto语句跳转到标签,进行代码间的无条件跳转.另外,goto语句在快速跳出循环.避免重复退出方面可以简化代码实现过程,但在结构化程序设计中一般不主张使用 ...
- Delphi 禁止重复运行程序的方法
第一种方法,使用"过程调用" procedure Del; // 自定义过程 var Mutex: THandle; begin Mutex := CreateMutex(nil, ...
- 【数据结构与算法】不同路径 III:使用哈密尔顿路径算法实现
[数据结构与算法]不同路径 III:使用哈密尔顿路径算法实现 Java 不同路径 III https://leetcode-cn.com/problems/unique-paths-iii/ 解题思路 ...
- DevOps常用工具网址
Linux基础和命令: shell语法查询: http://www.linux6.comhttps://www.tutorialspoint.com/linux_admin/index.htm 正则表 ...
- java基础之object类、Date类、System类、StringBuilder类、包装类、枚举类
一.public String toString() :默认返回该对象的字符串表示,其实该字符串内容就是对象的类型+@+内存地址值 重写后: @Override public String toStr ...
- Java高效合并Excel报表实战:GcExcel让数据处理更简单
前言:为什么需要自动化合并Excel? 在日常办公场景中,Excel报表合并是数据分析的基础操作.根据2023年企业办公效率报告显示: 财务人员平均每周花费6.2小时在Excel合并操作上 人工合并的 ...