13. Roman to Integer

Easy

Roman numerals are represented by seven different symbols: IVXLCD and 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. package leetcode.easy;
  2.  
  3. import java.util.HashMap;
  4.  
  5. public class RomanToInteger {
  6. @org.junit.Test
  7. public void test() {
  8. String s1 = "III";
  9. String s2 = "IV";
  10. String s3 = "IX";
  11. String s4 = "LVIII";
  12. String s5 = "MCMXCIV";
  13.  
  14. System.out.println(romanToInt(s1));
  15. System.out.println(romanToInt(s2));
  16. System.out.println(romanToInt(s3));
  17. System.out.println(romanToInt(s4));
  18. System.out.println(romanToInt(s5));
  19. }
  20.  
  21. public int romanToInt(String s) {
  22. if (null == s || 0 == s.length()) {
  23. return -1;
  24. }
  25. HashMap<Character, Integer> map = new HashMap<Character, Integer>();
  26. map.put('I', 1);
  27. map.put('V', 5);
  28. map.put('X', 10);
  29. map.put('L', 50);
  30. map.put('C', 100);
  31. map.put('D', 500);
  32. map.put('M', 1000);
  33.  
  34. int result = 0;
  35. result += map.get(s.charAt(s.length() - 1));
  36.  
  37. for (int i = s.length() - 2; i >= 0; i--) {
  38. if (map.get(s.charAt(i)) >= map.get(s.charAt(i + 1))) {
  39. result += map.get(s.charAt(i));
  40. } else {
  41. result -= map.get(s.charAt(i));
  42. }
  43. }
  44.  
  45. return result;
  46. }
  47. }

LeetCode_13. Roman to Integer的更多相关文章

  1. 【LeetCode】Roman to Integer & Integer to Roman

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

  2. 【leetcode】Integer to Roman & Roman to Integer(easy)

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

  3. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  4. 5.Integer to Roman && Roman to Integer

    Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...

  5. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  6. 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

    [本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...

  7. No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

  8. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

    12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...

  9. 3月3日(5) Roman to Integer

    原题 Roman to Integer 题意很简单,把Roman字母翻译成int. 实现方式也不难,针对每个字符转成int,从右往左,依次判断,如果当前值比上一个值大则相加,小则相减. 什么,你问我怎 ...

随机推荐

  1. C#中[STAThread]的作用

    [STAThread]STAThread:Single Thread Apartment Thread.(单一线程单元线程)[]是用来表示Attributes: [STAThread]是一种线程模型, ...

  2. c线程使用锁控制并发

    // // Created by gxf on 2019/12/16. // #include <stdlib.h> #include <stdio.h> #include & ...

  3. 标准键盘 acsii码值 表

    码值 含义 备注 0x08 Backspace键   0x09 Tab键   0x0C Clear键 Num Lock关闭时的数字键盘5 0x0D Enter键   0x10 Shift键   0x1 ...

  4. 深度解析Graph Embedding

    Graph Embedding是推荐系统.计算广告领域最近非常流行的做法,是从word2vec等一路发展而来的Embedding技术的最新延伸:并且已经有很多大厂将Graph Embedding应用于 ...

  5. PHP 获取上传文件的实际类型

    方案一: mime_content_type ( string $filename ) : string (PHP 4 >= 4.3.0, PHP 5, PHP 7) mime_content_ ...

  6. 第69题:x的平方根

    一. 问题描述 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输 ...

  7. docker压缩导入导出

    导出镜像 docker save <myimage>:<tag> | gzip > <myimage>_<tag>.tar.gz 导入镜像 gun ...

  8. 详解Python 切片语法

    Python的切片是特别常用的功能,主要用于对列表的元素取值.这篇文章主要介绍了详解Python 切片语法,需要的朋友可以参考下 Python的切片是特别常用的功能,主要用于对列表的元素取值.使用切片 ...

  9. LOJ2265. 「CTSC2017」最长上升子序列

    题意:中文题意很清楚 LOJ2263 分析: 根据Dilworth定理,最小链覆盖=最长反链. 问题转化为求 $k$ 个最小不上升序列能覆盖的最大数的个数. 参考链接: 1. https://blog ...

  10. [Google Guava] 9-I/O

    原文链接 译文链接 译者:沈义扬 字节流和字符流 Guava使用术语”流” 来表示可关闭的,并且在底层资源中有位置状态的I/O数据流.术语”字节流”指的是InputStream或OutputStrea ...