Facebook interview problem:13. Roman to Integer

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;
    }
}


原文地址:https://www.cnblogs.com/stiles/p/Leetcode13.html

时间: 2024-11-10 21:01:52

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

leetCode 13. Roman to Integer 字符串

13. Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 补充:罗马数字 罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

12. Integer to Roman &amp;&amp; 13. Roman to Integer &amp;&amp; 273. Integer to English Words

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

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 ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数

Leetcode 13. Roman to Integer(水)

13. Roman to Integer Easy 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 i

LeetCode 13. Roman to Integer

https://leetcode.com/problems/roman-to-integer/#/description Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 字符串简单题,要搞清楚转换规则.如果当前字母比前一个大,则相减,比如IV = 5 - 1:否则就相加,比如VI = 5 + 1,II = 1 + 1. 罗马数字_

LeetCode #13 Roman to Integer (E)

[Problem] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [Analysis] 建立一个字符到数字的map之后只需要抓住两个条件:1. 输入都是有效罗马数字; 2. 当小数字在大数字左边时需要用到减法. [Solution] public class Solution { public int romanToInt(St

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 from 1 to 3999. Subscribe to see which companies asked this questio 解析:给出一个罗马数字,要求把其转换为一个整数.输入范围在1到3999内. 罗马数字的规则如下: 罗马数字 I V X L C D M 代表的阿拉伯数字 1 5

【leetcode】13. Roman to Integer

题目描述: Given a roman numeral, convert it to an integer. 解题思路: 罗马计数法有以下规则: 基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: 不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: V 和 X 左边的小数字只能用 Ⅰ: L 和 C 左边的小数字只能用X: D

13 Roman to Integer (easy)

Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 简单介绍一下罗马数字,摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗马数字中没有“0”,与进位制无关.一般认为罗马数字只用来记数,而不作演算