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. connect to 10.104.11.128 port 9999 (tcp) failed: No route to host

    问题: iptables当找到匹配的规则时,就会执行相应的动作,而不会向下继续匹配. 可以看到https没有添加,匹配不到规则,所以就会包错 解决方法: iptables -I INPUT -p tc ...

  2. 5-----Scrapy框架中Spiders用法

    Spider类定义了如何爬去某个网站,包括爬取的动作以及如何从网页内容中提取结构化的数据,总的来说spider就是定义爬取的动作以及分析某个网页 工作流程分析 1.以初始的URL初始化Request, ...

  3. 分治法 - Divide and Conquer

    在计算机科学中,分治法是一种很重要的算法.分治法即『分而治之』,把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题的 ...

  4. 转 PyCharm 进行调试 以及怎么熟悉一个已经成熟的项目的代码和断点 以及 jetBrains pycharm快捷键

    https://blog.csdn.net/guider2334/rss/list Ctrl + Q 现实document视图,查看选择元素的详细信息        (重要) Ctrl + Alt + ...

  5. 如何将一个Maven项目转化成一个Eclipse项目

    有时候我们需要将一个Maven项目导入到Eclipse中,直接作为一个普通的eclipse项目来导入是不行的,我们可以通过一个命令来实现:mvn eclipse:eclipse 1. 进入该Maven ...

  6. c++ 封装线程库 2

    1.2线程回收: 首先得知道线程的两个状态: Joinable Detached 简单理解,如果一个线程是joinable的状态,那么这样的线程,就必须使用pthread_join来回收,否则程序结束 ...

  7. MATLAB特殊矩阵以及矩阵转置

    特殊矩阵 通用特殊矩阵 zeros函数:产生全0矩阵,即零矩阵. ones函数:产生....1矩阵,即幺矩阵. eye函数:产生对角线为1的矩阵,当矩阵是方正时,得到单位矩阵. rand函数:产生(0 ...

  8. Spark生态系统

    在大数据非常流行的今天,每个行业都在谈论大数据,每个公司(互联网公司,传统企业,金融行业等)都在讨论大数据.高层管理者利用大数据来进行决策:数据科学家利用大数据来进行业务创新:程序员利用大数据来完成项 ...

  9. 案例53-crm练习修改客户功能实现

    1 CustomerAction 完整代码: package www.test.web.action; import java.io.File; import org.apache.commons.l ...

  10. C语言实现通用链表初步(一)

    注意:本文讨论的是无头单向非循环链表. 假设不采用Linux内核链表的思路,怎样用C语言实现通用链表呢? 一种常用的做法是: typedef int element_t; struct node_in ...