Leetcode 12. Integer to Roman

12. Integer to Roman

题目描述

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 an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

思路

仔细看一下其实三个字母对应一位十进制数字,比如I和V都是对应到个位上的数字,当然特殊的9是用IX来表示的。所以我们可以一位一位地分开处理整个十进制数。
首先取出个位数字x,然后判断跟x的大小,等于9就是IX,等于5就是一个V,大于5就在V的右边添加x-5个I,等于4就是IV,小于4就x个I
x = { I X if  x = 9 V I . . . I ⎵ x-5 if  9 &gt; x &gt; 5 V if  x = 5 I V if  x = 4 I . . . I ⎵ x if  x &lt; 4 x = \begin{cases} IX &amp;\text{if } x = 9 \\ V \underbrace{I...I}_{\text{x-5}} &amp;\text{if } 9 &gt; x &gt; 5 \\ V &amp;\text{if } x = 5 \\ IV &amp;\text{if } x = 4 \\ \underbrace{I...I}_{\text{x}} &amp;\text{if } x &lt; 4 \end{cases} x=IXVx-5 I...IVIVx I...Iif x=9if 9>x>5if x=5if x=4if x<4
如果换到十位数字就是把上表中的X、V、I换成C、L、X
然后百位数字就是换成M、D、C
千位数字只到3,就是说只可能是x个M

代码

class Solution {
public:
    string intToRoman(int num) {
      // M, D, C, L, X, V, I
      char c[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
      int scale = 10;
      string result;
      
      for (int i = 0; i < 4; i++) {
        int x = num % scale;
        if (x == 9) {
          result.append(1 ,c[2 * i + 2]);
          result.append(1 ,c[2 * i]);
        }
        else if (x > 5) {
          for (int j = 0; j < x - 5; j++) {
            result.append(1 ,c[2 * i]);
          }
          result.append(1 ,c[2 * i + 1]);
        } else if (x == 5) {
          result.append(1 ,c[2 * i + 1]);
        } else if (x == 4) {
          result.append(1 ,c[2 * i + 1]);
          result.append(1 ,c[2 * i]);
        } else {
          for (int j = 0; j < x; j++) {
            result.append(1 ,c[2 * i]);
          }
        }
        num /= scale;
      }
      reverse(result.begin(),result.end());
      return result;
      // return strrev(result);
    }
};

Runtime: 40 ms, faster than 97.19% of C++ online submissions for Integer to Roman.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值