LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字。阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则。Wiki了一把,又参考了其它的文档,总结如下:

罗马数字规则:

1, 罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。

罗马数字中没有“0”。

2, 重复次数:一个罗马数字最多重复3次。

3, 右加左减:

在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。

在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。

4, 左减的数字有限制,仅限于I、X、C,且放在大数的左边只能用一个。

(*) V 和 X 左边的小数字只能用Ⅰ。

       (*) L 和 C 左边的小数字只能用X。

      (*) D 和 M 左 边的小数字只能用C。

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

int romanToInt(string s) {
        map<char, int> dct;
        dct['I'] = 1, dct['i'] = 1;
        dct['V'] = 5, dct['v'] = 5;
        dct['X'] = 10, dct['x'] = 10;
        dct['L'] = 50, dct['l'] = 50;
        dct['C'] = 100, dct['c'] = 100;
        dct['D'] = 500, dct['d'] = 500;
        dct['M'] = 1000, dct['m'] = 1000;

        int sum = 0, j;
        for(int i = 0; i < s.size(); ++i)
        {
            j = i+1;

            if(j < s.size() && dct[s[j]] > dct[s[i]])
            {
                sum += dct[s[j]] - dct[s[i]];
                i = j;
            }
            else
                sum += dct[s[i]];
        }
        return sum;
    }

代码实际上很简单,向前看一位。比较当前位及下一位的值,再确定是加还是减。

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

struct node{
      int key;
      string szRoman;
      node(int k, string s):key(k), szRoman(s){}
    };

    string intToRoman(int num){
        vector<node> dct;
        dct.push_back(node(1000, "M"));
        dct.push_back(node(900,  "CM"));
        dct.push_back(node(500,  "D"));
        dct.push_back(node(400,  "CD"));
        dct.push_back(node(100,  "C"));
        dct.push_back(node(90,   "XC"));
        dct.push_back(node(50,   "L"));
        dct.push_back(node(40,   "XL"));
        dct.push_back(node(10,   "X"));
        dct.push_back(node(9,    "IX"));
        dct.push_back(node(5,    "V"));
        dct.push_back(node(4,    "IV"));
        dct.push_back(node(1,    "I"));

        string res;
        int i = 0;
        while(num > 0)
        {
            if(num/dct[i].key == 0)
            {
                i += 1;
                continue;
            }

            for(int j = 0; j < num/dct[i].key; ++j)
                res.append(dct[i].szRoman);

            num%=dct[i].key;
        }
        return res;
    }

本人偷懒了,先搞了一张表,然后遍历表。表中比较特殊的值有900, 400, 90, 40, 9, 4,这些都是根据规则4得来的。

参考文献:

http://www.onlineconversion.com/roman_numerals_advanced.htm

LeetCode之Integer to Roman, Roman to Integer,布布扣,bubuko.com

时间: 2024-08-02 02:42:28

LeetCode之Integer to Roman, Roman to Integer的相关文章

【leetcode刷题笔记】Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字:否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字.利用一个HashMap存放罗马字符和数字的对应. 罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.

Integer to Roman &amp; Roman to Integer

Integer to Roman Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range from 1 to 3999. Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals https://zh.wikipedia.org/wiki/%E7%BD%9

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

LeetCode第[7]题(Java):Reverse Integer 标签:数学

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum

Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?

Integer.valueof(String s)是将一个包装类是将一个实际值为数字的变量先转成string型再将它转成Integer型的包装类对象(相当于转成了int的对象)这样转完的对象就具有方法和属性了.而Integer.parseInt(String s)只是将是数字的字符串转成数字,注意他返回的是int型变量不具备方法和属性. 设有下面两个赋值语句:a=Integer.parseInt(“123”);b=Integer.valueOf(“123”).intValue();下述说法正确的

leetcode修炼之路——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从零单刷】Roman to Integer

题目: 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). 右加左減: 在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數

LeetCode 12-13:Integer to Roman&amp;&amp;Roman to Integer

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX&qu

LeetCode12~14 Integer to Roman/Roman to Integer/Longest Common Prefix

一:Integer to Roman 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 链接:https://leetcode.com/problems/integer-to-roman/ 分析:此题关键是确定罗马数字如何表示,比如4并不是IIII而是IV,9并不是VIIII而是IX, 通过不断的取出最高位和余下的得到结果