Leetcode 12. Integer to Roman (Medium) (cpp)

来源:互联网 发布:mac 装虚拟机 有影响吗 编辑:程序博客网 时间:2024/06/05 15:47

Leetcode 12. Integer to Roman (Medium) (cpp)

Tag: Math, String

Difficulty: Medium


/*12. Integer to Roman (Medium)Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.*/class Solution {public:string intToRoman(int num) {string res;char* c[4][10] = {{"", "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"}};res += c[3][num / 1000 % 10];res += c[2][num / 100  % 10];res += c[1][num / 10   % 10];res += c[0][num        % 10];return res;}};


0 0
原创粉丝点击