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. POJ 3067 Japan (树状数组 && 控制变量)

    题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条 ...

  2. poj 3977 子集

    题目 题意:在一个集合中找到一个非空子集使得这个子集元素和的绝对值尽量小,和绝对值相同时保证元素个数尽量小 分析:1.二分枚举的思想,先分成两个集合: 2.枚举其中一个集合中所有的子集并且存到数组中, ...

  3. Jenkins自动化CI CD流水线之2--用户权限管理

    一. 背景 针对开发.运维.测试针对不同角色进行不同权限划分, 基于插件: Role-based Authorization Strategy来实现. 一. 安装 安装该插件: 系统管理->管理 ...

  4. win10下Anaconda3在虚拟环境python_version=3.5.3 中配置pyspark

    1. 序经过了一天的修炼,深深被恶心了,在虚拟环境中配置pyspark花式报错,由于本人实在是不想卸载3.6版的python,所以硬刚了一天,终于摸清了配置方法,并且配置成功,不抱怨了,开讲: 2. ...

  5. 02Data

    1.数据从何而来 2.数据对象和属性类型 数据集合的类型 结构数据的重要特征 数据对象 属性 属性类型 数据属性的类型 离散 vs.连续属性 3.数据的(基本)统计描述 分布度量 代数度量 整体度量 ...

  6. CSS文件的三种引入方式

    CSS的引入方式共有三种:行内样式.内部样式表.外部样式表. 一.行内样式 使用style属性引入CSS样式. 示例:<h1 style="color:red;">st ...

  7. JS你可能还不知道的一些知识点(一)

    js程序是用Unicode字符集编写的, 2.转义字符:反斜线 1 2 3 4 function Test(){   var s='you\'re right,it can\'t be a quote ...

  8. LitJson(读Exce文件写入到json文件):

    读Exce文件写入到json文件汇总: //命名空间 using System.Collections; using System.Collections.Generic; using System. ...

  9. (转)cut命令详解

    Linux:cut命令详解   cut 文件内容查看 显示行中的指定部分,删除文件中指定字段 显示文件的内容,类似于下的type命令. 说明 该命令有两项功能,其一是用来显示文件的内容,它依次读取由参 ...

  10. C#:新邮件监听及搜索

    在项目中,我们需要监听邮件服务器,看是否有新的邮件进入.下面的代码可以帮助我们监听新邮件,并对已有的邮件进行查找: using System; using System.Collections.Gen ...