description:

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
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:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. -----------------------------------------------------------------------------
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
class Solution {
//pre store them first
public int romanToInt(String s) {
Map<Character, Integer> table = new HashMap<>();
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);
//s is not null
//string operation
int res = 0;
for(int i = 0; i<s.length(); i++){
char ele = s.charAt(i);
if(ele == 'I' && i<s.length()-1){
i++;
char ele1 = s.charAt(i);
if(ele1=='V') res+=4;
else if(ele1 == 'X') res+=9;
else{
i--;
res+=table.get(ele);
}
}else if(ele == 'X' && i<s.length()-1){
i++;
char ele1 = s.charAt(i);
if(ele1=='L') res+=40;
else if(ele1 == 'C') res+=90;
else{
i--;
res+=table.get(ele);
}
}else if(ele == 'C' && i<s.length()-1){
i++;
char ele1 = s.charAt(i);
if(ele1=='D') res+=400;
else if(ele1 == 'M') res+=900;
else{
i--;
res+=table.get(ele);
}
}else {
res += table.get(ele);
}
}
return res;
}
}
												

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. HDU 4507 求指定范围内与7不沾边的所有数的平方和 (数位DP)

    题意:求区间[l,r]内所有与7无关的数的平方和(取模)定义与7无关的数:                                      1.数字的数位上不能有7              ...

  2. CF D Bicolorings

    题意 给一个2行n列的矩阵填上黑色和白色,求连通块个数为k个的填色方案数量(mod 998244353)   因为只有两行,为n-1列的矩阵增加1列的情况数只有很少,容易想到用 (i,k) 表示 i  ...

  3. dedecms模板目录

    根目录 /dede 管理后台目录 /freelist 自由文档列表生成目录 /html 默认文章生成目录 /include 程序核心文件目录 /member 会员管理目录 /plus 插件及辅助功能目 ...

  4. tp5分组查询

    $data=DB::name('goods_common')->alias('a')->join('all580_goods_attractions w','a.common_id = w ...

  5. jinkens + svn 把代码更新到本地在运行。(方法比较笨,只是想实现自己的想法。把代码更新下来在运行。)

    jinkens + svn 把代码更新到本地在运行.

  6. restTemplate使用

    转载博主: https://blog.csdn.net/itguangit/article/details/78825505 https://www.cnblogs.com/zhaoyan001/p/ ...

  7. 第五章:引用类型(一)-Object和Array

    引用类型 引用类型的值(对象)是引用类型的一个实例 引用类型是一种数据结构,用于将数据与功能组织在一起 也常被称为类, Object 对象的两种创建方式 使用new操作符 对象字面量表示法 Array ...

  8. 在本地安装oracle-maven库

    mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging= ...

  9. spring整合jpa

    有点类似SSH整合,O(∩_∩)O哈哈~ <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  10. Android NDK开发 字符串(四)

    几个概念首先要明确: java内部是使用16bit的unicode编码(UTF-16)来表示字符串的,无论中文英文都是2字节: jni内部是使用UTF-8编码来表示字符串的,UTF-8是变长编码的un ...