Leetcode 12 Integer to Roman

来源:互联网 发布:新闻联播图片素材软件 编辑:程序博客网 时间:2024/06/16 13:17

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 table[4][11]={            {"", "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 result = "";          int now=0;          while (num > 0)         {              result=table[now][num%10]+result;            num/=10;              now++;          }          return  result;    }};


0 0
原创粉丝点击