[LeetCode]13. Roman to Integer罗马数字转整数

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 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: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

跟上一题相反,要把罗马数字转成阿拉伯数字,观察之后我们发现,可以从左到右遍历罗马数字的字符串,无非两种可能1.当前字符是最后一位或者表示数字不小于后面一位字符表示数字,类似VII,就是往上加,先是5,然后是5+1,最后6+12.当前字符表示数字比后一位字符小,类似IV,就需要减去当前位的数字
class Solution {
    public int romanToInt(String s) {
        if (s == null || s.length() == 0)    return -1;
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        map.put(‘I‘, 1);
        map.put(‘V‘, 5);
        map.put(‘X‘, 10);
        map.put(‘L‘, 50);
        map.put(‘C‘, 100);
        map.put(‘D‘, 500);
        map.put(‘M‘, 1000);
        int res = 0;
        for(int i=0;i<s.length();i++){
            int val=map.get(s.charAt(i));
            if(i==s.length()-1 || map.get(s.charAt(i+1))<=map.get(s.charAt(i))){
                res=res+val;
            }else{
                res=res-val;
            }
        }
        return res;

    }
}

原文地址:https://www.cnblogs.com/jchen104/p/10212019.html

时间: 2024-11-09 03:06:51

[LeetCode]13. Roman to Integer罗马数字转整数的相关文章

LeetCode 13.Roman to Integer 罗马数字转阿拉伯数字

对于罗马数字转阿拉伯数字切入点有两个: 1.小数字出现在大数字前面只能使用一个(例如IV正确,IIV就是错误的) 2.除了情况1之外直接使用累加就ok 1 public int romanToInt(String s){ 2 if(s.length() < 1){ 3 return 0; 4 } 5 int result = 0; 6 for(int i=0;i<s.length();i++){ 7 result += getRomanValue(s.charAt(i)); 8 if(i<

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 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).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

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 C语言

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 罗马数字与整数互转 int getInt(char c){     int temp;     switch(c){         case 'I':return 1;         case 'V':return 5;         case 'X':return 10;  

LeetCode 13 Roman to Integer 解题报告

题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand 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, whi

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 - 思考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解析 罗马字符转int类型关于栈的常数实现-Java实现

Q: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. A: 以下解法和代码没有借阅以往任何资料,如果有更好的解法请在评论区留言 看到这一题我真是感叹城会玩,昨天刚刚解过一个int转罗马字母,今天又反过来解.自叹弗如.这题的该意思就是给一个罗马字符,把他转化成一个int数字,范围在1-3999之间.关于罗马数字的详细信息就不复制了,