
【leetcode】罗马数字转整数
������������������������������������������������������������������������������������������������������������������������ ��������������������������� ������������������������������������������������������������������������������������������������ ������������������������������������������������������������������������������������������������������������������������������������������������������������ ������ ��������������� ������������������ ������������������������������������������������������������������������ ��������������������������������������������������������������� ������������������������������������������������������������������������������������������������������������������������������������������ ���������������������������������������������������
发布日期:2021-05-12 05:47:21
浏览次数:23
分类:精选文章
本文共 2504 字,大约阅读时间需要 8 分钟。
���������������������������������������������������������������������������
total
���������������������������previous
������������������������������������total
���������������previous
���total
���������������������������������������������������������������������total
������previous
������������������������������������previous
���������������������������������������
import java.util.HashMap;import java.util.Map;public class Solution { public int romanToInt(String s) { MapromanMap = new HashMap<>(); romanMap.put('I', 1); romanMap.put('V', 5); romanMap.put('X', 10); romanMap.put('L', 50); romanMap.put('C', 100); romanMap.put('D', 500); romanMap.put('M', 1000); int total = 0; int prev = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); int current = romanMap.get(c); if (i == 0) { total += current; prev = current; } else { if (current <= prev) { total += current; } else { total += current - 2 * prev; } prev = current; } } return total; }}
������������
romanMap
������������������������������������������������������������total
������������0���������������������������������������prev
������������0������������������������������������������total
���������������prev
������������������total
������������������������������������������������������������������������total
������������������prev
���������������������������������������������������������������������������������������������������������������
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月05日 04时32分45秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
JS将数组中的字符串按字母表排序
2021-05-12
springboot 项目 pom文件报错 Unknown error
2021-05-12
Dots杂项(不定期更新)
2021-05-12
第n高的薪水 sql查询
2021-05-12
node-sass安装错误,提示缺少python2包
2021-05-12
什么样的数据才有价值?应该怎样收集和处理?
2021-05-12
VS中 fatal error LNK1123: 转换到 COFF 期间失败 的解决方法
2021-05-12
关于Img标签在固定宽高的容器内部以图片比例缩放存在
2021-05-12
Anaconda3 安装 爬虫库 selenium
2021-05-12
python --- 监控客户端是否存活
2021-05-12
pyhton---异常处理的终极语法、网页访问基本读取、网页访问异常处理
2021-05-12
Centos 7.3 计算本目录下的以特定名字文件夹个数
2021-05-12
linux下编程出现 对'sem_wait'未定义的引用解决方案
2021-05-12
[每日一题] 85. 红与黑(图、DFS)
2021-05-12
[LeetCode 双周赛22] 2. 安排电影院座位(排序、暴力优化、巧妙解法)
2021-05-12
JavaFX学习笔记-颜色选择器ColorPicker与日期选择器DatePicker
2021-05-12
工具研究:(三)Nginx配置错误的路由时均统一跳转到登录界面
2021-05-12