Roman numerals are represented by seven different symbols: IVXLCDand M.

  1. Symbol Value
  2. I 1
  3. V 5
  4. X 10
  5. L 50
  6. C 100
  7. D 500
  8. M 1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

  1. Input: "III"
  2. Output: 3

Example 2:

  1. Input: "IV"
  2. Output: 4

Example 3:

  1. Input: "IX"
  2. Output: 9

Example 4:

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

Example 5:

  1. Input: "MCMXCIV"
  2. Output: 1994
  3. Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
  1. class Solution {
  2. public int romanToInt(String s) {
  3. if (s == null || s.length() == 0) {
  4. return 0;
  5. }
  6. Map<Character, Integer> myMap = getMap();
  7. int res = myMap.get(s.charAt(0));
  8. for(int i = 1; i < s.length(); i++) {
  9. // for case of IV = 1 + 5 - 2 * 1
  10. if (myMap.get(s.charAt(i)) > myMap.get(s.charAt(i - 1))) {
  11. res += myMap.get(s.charAt(i)) - 2 * myMap.get(s.charAt(i - 1));
  12. } else {
  13. res += myMap.get(s.charAt(i));
  14. }
  15. }
  16. return res;
  17. }
  18.  
  19. private Map<Character, Integer> getMap() {
  20. Map<Character, Integer> myMap = new HashMap<>();
  21. myMap.put('I', 1);
  22. myMap.put('V', 5);
  23. myMap.put('X', 10);
  24. myMap.put('L', 50);
  25. myMap.put('C', 100);
  26. myMap.put('D', 500);
  27. myMap.put('M', 1000);
  28. return myMap;
  29. }
  30. }

[LC] 13. Roman to Integer的更多相关文章

  1. Leetcode#13. Roman to Integer(罗马数字转整数)

    题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  4. C# 写 LeetCode easy #13 Roman to Integer

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

  5. 13. Roman to Integer【leetcode】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  6. 【LeetCode】13. Roman to Integer (2 solutions)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  7. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

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

  9. 13. Roman to Integer[E]罗马数字转整数

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

随机推荐

  1. UVA 10534 LCS变种题

    求一个序列中 的2*n-1个数字 ,前n+1个数字为严格升序 后n+1个为严格降序,求最长的长度 一开始还没想清楚怎么解,其实就是一个LCS问题,从头到尾以及反序求一下LCS 由于 d[i]为包含了自 ...

  2. 【Pytyon模块】logging模块-日志处理

    一.日志相关概念 1.日志的作用 通过log的分析,可以方便用户了解系统或软件.应用的运行情况:如果你的应用log足够丰富,也可以分析以往用户的操作行为.类型喜好.地域分布或其他更多信息:如果一个应用 ...

  3. 吴裕雄--天生自然MySQL学习笔记:MySQL NULL 值处理

    MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. 为了处理这种情况,MySQL提供了三大运算符 ...

  4. priority_queue(优先队列)的用法(包括pbds)

    置顶!!! 有时候在定义的时候,不要把两个<>连在一起写,以免被编译器错误理解!!!! 头文件 #include <queue> queue的一般用法不再叙述 类型名 prio ...

  5. 输入pip命令报错:from pip import main ImportError: cannot import name 'main'

    报错信息: qly@qlyComputer:~$ pip Traceback (most recent call last): File "/usr/bin/pip", line ...

  6. dp--最长上升子序列LIS

    1759:最长上升子序列 总时间限制:  2000ms 内存限制:  65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对 ...

  7. $_SESSION $_COOKIE

    $_SESSION是临时会话变量,用来储存访问者信息.内容是储存在服务器上面的.比如 $_SESSION["ABC"] = "aaa";那么这个用户访问时,$_ ...

  8. java.sql.BatchUpdateException: ORA-01691: Lob 段 CSASSSMBI.SYS_LOB0000076987C00003$$ 无法通过 128 (在表空间 HRDL_CSASS 中) 扩展

    问题: 在tomcat日志信息中出现:java.sql.BatchUpdateException: ORA-01691: Lob 段 CSASSSMBI.SYS_LOB0000076987C00003 ...

  9. Promoter complex|转录组水平RNA的复杂度|

    生命组学 Promoter complex Tata box识别位点 Enhancer加入之后增强转录 不确定性与确定性之间的关系,原因中存在这不确定性,但是结果表达又是确定的.因为promoter的 ...

  10. redis 会丢数据吗

    不管是以前的主从模式(哨兵模式),还是现在的集群模式,因为都用了slave of 同步; 而slave of 同步会丢弃本地数据,直接用对方的数据来覆盖本地,所以会丢失数据 1.主备网络不通,后续主节 ...