description:

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: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

  1. Input: "MCMXCIV"
  2. Output: 1994
  3. Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
  4.  
  5. -----------------------------------------------------------------------------
    It is definitely not hard problem but hard to understand.
    IV:4 XL:40 CD:400  !!!!!!!!!!!!
    IX:9 XC:90 CM:900 !!!!!!!!!!!!
    just determine the case of I, X, C
  1. class Solution {
  2. //pre store them first
  3. public int romanToInt(String s) {
  4. Map<Character, Integer> table = new HashMap<>();
  5. table.put('I',1);table.put('V',5);table.put('X',10);table.put('L',50);table.put('C',100);table.put('D',500);table.put('M',1000);
  6. //s is not null
  7. //string operation
  8. int res = 0;
  9. for(int i = 0; i<s.length(); i++){
  10. char ele = s.charAt(i);
  11. if(ele == 'I' && i<s.length()-1){
  12. i++;
  13. char ele1 = s.charAt(i);
  14. if(ele1=='V') res+=4;
  15. else if(ele1 == 'X') res+=9;
  16. else{
  17. i--;
  18. res+=table.get(ele);
  19. }
  20. }else if(ele == 'X' && i<s.length()-1){
  21. i++;
  22. char ele1 = s.charAt(i);
  23. if(ele1=='L') res+=40;
  24. else if(ele1 == 'C') res+=90;
  25. else{
  26. i--;
  27. res+=table.get(ele);
  28. }
  29. }else if(ele == 'C' && i<s.length()-1){
  30. i++;
  31. char ele1 = s.charAt(i);
  32. if(ele1=='D') res+=400;
  33. else if(ele1 == 'M') res+=900;
  34. else{
  35. i--;
  36. res+=table.get(ele);
  37. }
  38. }else {
  39. res += table.get(ele);
  40. }
  41. }
  42. return res;
  43. }
  44. }
  1.  

Facebook interview problem:13. Roman to Integer的更多相关文章

  1. [LeetCode&Python] Problem 13. Roman to Integer

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

  2. 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 ,即 ...

  3. Leetcode 13. Roman to Integer(水)

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

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

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

  5. 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  ...

  6. 13. Roman to Integer【leetcode】

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

  7. 【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 ...

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

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

  9. 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 ...

随机推荐

  1. Experimental Educational Round: VolBIT Formulas Blitz B

    Description The city administration of IT City decided to fix up a symbol of scientific and technica ...

  2. Laravel5.1 目录结构解析

    学习一门框架,首先要了解的就是目录结构.对目录结构清晰就可以着手学习了~这里不作新特性的介绍,权当目录结构手册看吧.若发现有何不恰当的地方请联系我哦~注:写本文时参照的是5.1.4版本 目录或文件 说 ...

  3. redis备份恢复

    redis的几种数据导入导出方式[转]   环境说明:202.102.221.11 redis源实例202.102.221.12 redis目标实例202.102.221.13 任意linux系统 一 ...

  4. django ORM 连表查询2

    set() 更新model对象的关联对象 book_obj=models.Book.objects.first() book_obj.authors.set([2,3]) 把book_obj这个对象重 ...

  5. C语言标准库函数memcpy和memmove的区别以及内存重叠问题处理

    ①memcpy()和memmove()都是C语言中的标准库函数,定义在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, cons ...

  6. hdu 6288(二分法加精度处理问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6288 题意:给出a,b,k,n可满足(n^a)*(⌈log2n⌉)^b<=k ,求最大的n值三个 ...

  7. java——编译和运行

    Java源代码---->编译器---->Java字节码(即虚拟指令..class文件.特殊的二进制文件.二进制字节码文件)---->jvm---->解释器(jvm的一部分)-- ...

  8. Python 3.6 TypeEror: iter() returned non-iterator of type

    环境:Python 3.6 class Fabs(object): def __init__(self,max): self.max = max self.n, self.a, self.b = 0, ...

  9. SQL Server Reporting Service(SSRS) 第六篇 SSRS 部署总结

    前段时间完成了第一批次SSRS报表的开发,本来以为大功已经告成,结果没有想到在整个发布与部署过程中还是遇到了很多的问题,现将这些问题一一列举出来,希望对以后能够有所启发! 1. 关于数据源与数据集的发 ...

  10. Linux文件操作常用选项

    常用选项 选项 功能 -a 查看隐藏文件 -l 列表方式查看 -h 人性化显示 * 通配符,忽略多个字符匹配 ? 通配符,忽略一个字符匹配 [Num1-Num2] 通配符,查看从Num1到Num2的匹 ...