Facebook interview problem:13. Roman to Integer
description:
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
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 beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(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的更多相关文章
- [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 ...
- 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 ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- 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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【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 ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
随机推荐
- Experimental Educational Round: VolBIT Formulas Blitz B
Description The city administration of IT City decided to fix up a symbol of scientific and technica ...
- Laravel5.1 目录结构解析
学习一门框架,首先要了解的就是目录结构.对目录结构清晰就可以着手学习了~这里不作新特性的介绍,权当目录结构手册看吧.若发现有何不恰当的地方请联系我哦~注:写本文时参照的是5.1.4版本 目录或文件 说 ...
- redis备份恢复
redis的几种数据导入导出方式[转] 环境说明:202.102.221.11 redis源实例202.102.221.12 redis目标实例202.102.221.13 任意linux系统 一 ...
- django ORM 连表查询2
set() 更新model对象的关联对象 book_obj=models.Book.objects.first() book_obj.authors.set([2,3]) 把book_obj这个对象重 ...
- C语言标准库函数memcpy和memmove的区别以及内存重叠问题处理
①memcpy()和memmove()都是C语言中的标准库函数,定义在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, cons ...
- hdu 6288(二分法加精度处理问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6288 题意:给出a,b,k,n可满足(n^a)*(⌈log2n⌉)^b<=k ,求最大的n值三个 ...
- java——编译和运行
Java源代码---->编译器---->Java字节码(即虚拟指令..class文件.特殊的二进制文件.二进制字节码文件)---->jvm---->解释器(jvm的一部分)-- ...
- 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, ...
- SQL Server Reporting Service(SSRS) 第六篇 SSRS 部署总结
前段时间完成了第一批次SSRS报表的开发,本来以为大功已经告成,结果没有想到在整个发布与部署过程中还是遇到了很多的问题,现将这些问题一一列举出来,希望对以后能够有所启发! 1. 关于数据源与数据集的发 ...
- Linux文件操作常用选项
常用选项 选项 功能 -a 查看隐藏文件 -l 列表方式查看 -h 人性化显示 * 通配符,忽略多个字符匹配 ? 通配符,忽略一个字符匹配 [Num1-Num2] 通配符,查看从Num1到Num2的匹 ...