LeetCode全集请参考:LeetCode Github 大全

算法

13. Roman to Integer

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

Example 1:

Input: s = "III"
Output: 3

Example 2:

Input: s = "IV"
Output: 4

Example 3:

Input: s = "IX"
Output: 9

Example 4:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

1 <= s.length <= 15

s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].

Map记录解法

  1. 先用map记录对应的char和数字。
  2. 如果前一个字母比后一个字母小,则说明是减法。
  3. 最后一个字母累加即可。
class Solution {public int romanToInt(String s) {Map<Character, Integer> map = new HashMap<>();map.put('I', 1);map.put('V', 5);map.put('X', 10);map.put('L', 50);map.put('C', 100);map.put('D', 500);map.put('M', 1000);char[] chars = s.toCharArray();int sum = 0;for(int i = 0; i < chars.length - 1; i++) {if (map.get(chars[i]) < map.get(chars[i + 1])) {sum -= map.get(chars[i]);} else {sum += map.get(chars[i]);}}sum += map.get(chars[chars.length - 1]);return sum;}
}

算法:罗马数字转换为整数13. Roman to Integer相关推荐

  1. LeetCode 13. Roman to Integer

    问题链接 LeetCode 13. Roman to Integer 题目解析 将罗马数字转换成普通数字. 解题思路 先简单了解一下什么是罗马数字. 基本字符:I,V,X,L,C,D,M 相应的阿拉伯 ...

  2. 算法 罗马数字转整数

    算法 罗马数字转整数 1.题目 2.方法 2.1枚举法(没得办法) 2.2map(大神级别) 1.题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 ...

  3. Leetcode典型题解答和分析、归纳和汇总——T12(罗马数字转换为整数)

    题目描述: 罗马数字包含以下七种字符:IVXLCDM 问题解析: [1]通过构建hashmap的键值对的查询关系,可以获得罗马数字与整数之间的关系: [2]处理特殊的规则:逐个识别罗马字符串s(从左至 ...

  4. C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3842 访问. 罗马数字包含以下七种字符: I, V, X, L, ...

  5. leetcode python3 简单题13. Roman to Integer

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十三题 (1)题目 英文: Given a roman numeral, conv ...

  6. 【LeetCode】13. Roman to Integer

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  7. 13. Roman to Integer

    1.问题描述 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Val ...

  8. python实现罗马数字转换为整数

    题目描述 上一篇文章中,我们介绍了将阿拉伯数字转换为罗马数字的方法,里面详细介绍了罗马数字的书写规则,请参考python实现将整数转换为罗马数字 LeetCode原题地址:https://leetco ...

  9. Java算法罗马数字转整数

    本文章只提供算法. 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符          数值 I             1 V             5 X         ...

  10. LeetCode: 13. Roman to Integer

    051106 题目 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol ...

最新文章

  1. java后台分页插件怎么写_Java分页技术(从后台传json到前台解析显示)
  2. html中事件调用JavaScript函数时有return与没有return的区别
  3. SitemapBaiduspider
  4. java 中都有什么结构_java中都有哪些数据结构?
  5. 提高电脑反应速度_宁美千元价电脑,一体机设计+21英寸屏,办公娱乐选它
  6. DIV+CSS两种盒子模型(W3C盒子与IE盒子)
  7. chrome vue.js插件文档_神级宝库!GitHub 标星 1.2w+,Chrome 最天秀的插件都在这里啦!...
  8. 对Docker常用命令的整理
  9. 如何在不重新加载页面的情况下修改URL?
  10. 计算机应用的问答题,计算机应用技术问答题(一)
  11. Java混乱的日志体系(logback)(转)
  12. android 获取service 实例化,在Activity中,如何获取service对象?a.可以通过直接实例化得到。b.可以通过绑定得到。c.通过star - 众答网问答...
  13. 软件工程课程实验报告:实验五
  14. android短信验证码免费版,短信验证码功能-免费哦!亲测可用
  15. 基于模型的约束排序,并探究OTUs对pH的响应特征——单峰or线性?
  16. a标签中的href=javascript
  17. 八、python爬虫伪装 [免费伪装ip伪装请求头]
  18. 小米6与xp系统不能连接服务器,小米6如何连接XP电脑
  19. HTML+CSS 编辑的(多列布局、相册、百度首页)、盒子模型
  20. java导出excel 边框不全_POI 导出Excel合并单元格后部分边框不显示

热门文章

  1. apache tomcat linux 配置,linux下apache+tomcat配置
  2. zabbix2.4监控mysql_Zabbix 2.4.5自带MySQL监控的配置使用教程
  3. python多维数据聚类可视化_基于python3的可视化数据聚类系统(k-means算法和k-中心点算法)...
  4. docker tensorflow_用Docker容器方式安装TensorFlow
  5. davinci项目服务器无法,【工程管理】为达芬奇建一个项目管理服务器 多人协同调色...
  6. mysqli_connect函数未开启_CRMEB打通版linux系统部署客服开启长链接不成功
  7. css选择器总结(内附实例及截图)
  8. 完整的vue-cli3项目创建过程以及各种配置
  9. php使用到的函数记录一
  10. 深入理解JavaScript系列(31):设计模式之代理模式