LeetCode12. Integer to Roman

来源:互联网 发布:魔兽世界 70数据库 编辑:程序博客网 时间:2024/05/16 00:38

题目链接:

https://leetcode.com/problems/integer-to-roman/

题目描述:

将一个整数转化成罗马数字字符串。

题目分析:

【罗马数字】

1~9: {“I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”};

10~90: {“X”, “XX”, “XXX”, “XL”, “L”, “LX”, “LXX”, “LXXX”, “XC”};

100~900: {“C”, “CC”, “CCC”, “CD”, “D”, “DC”, “DCC”, “DCCC”, “CM”};

1000~3000: {“M”, “MM”, “MMM”}.

个位十位百位千位,分成4组。用vector<vector<string>>

代码:

class Solution {public:     vector<vector<string>> roman = {              {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},              {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},              {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},              {"", "M", "MM", "MMM"}          };      string intToRoman(int num) {        string result="";        int cnt=0;        while(num){            result=roman[cnt++][num%10]+result;            num/=10;        }        return result;    }};
0 0
原创粉丝点击