LeetCode 12 Integer to Roman

Given an integer, convert it to a roman numeral.

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

原文的意思很简单,就是将输入的整型变量转化为罗马数字进行输出,整型变量和罗马数字的转换关系如下所示:

I ( 1 ) I (1) I(1), V ( 5 ) V(5) V(5), X ( 10 ) X (10) X(10), L ( 50 ) L(50) L(50), C ( 100 ) C(100) C(100), D ( 500 ) D(500) D(500), M ( 1000 ) M(1000) M(1000)

例如: 2341 -> MMCCCXLI 3021 -> MMMXXI

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。

C++ (2ms):

class Solution {
public:
    string intToRoman(int num) {
        stringstream res;
        char roman_numeral[] = {'M','D','C','L','X','V','I'};
        int integer[] = {1000,500,100,50,10,5,1};
        vector<pair<int,char>> integerToroman;
        for(int i=0; i<7; ++i){
            integerToroman.push_back(make_pair(integer[i],roman_numeral[i]));
        }
        int stage;
        for(int j=0;j<7;j+=2)
	{
	  if(float(num)/float(integer[j]) >= 1){
	    stage=j;
	    cout<<num<<endl;
	    cout<<stage<<endl;
	    break;
	  }
	}
        for(int j=stage; j<integerToroman.size(); j+=2)
	{
	  int result = num/integerToroman[j].first;
	  if(result<4){
	    for(int i=0;i<result;i++){
	      res << integerToroman[j].second;
	    }
	  }
	  if(result==4){
	      res << integerToroman[j].second << integerToroman[j-1].second;
	  }
	  if(result>4 && result<9){
	      res << integerToroman[j-1].second;
	      for(int k=5;k<result; k++){
	      res << integerToroman[j].second;
	      }
	  }
	   if(result==9){
	     res << integerToroman[j].second << integerToroman[j-2].second;
	  }
	  num = num % integerToroman[j].first;
	}
	
	return (res.str());
    }
};

Python (30ms)

class Solution(object):
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        roman_numeral=['M','D','C','L','X','V','I']
        integer=[1000,500,100,50,10,5,1]
        stage=0
        res=""
        for i in range(len(integer),2):
            if float(num)/float(integer[i]) >=1:
                stage=i
                break
                
        for j in range(stage,len(integer),2):
            result=num/integer[j]
            if result<4:
                for k in range(result):
                    res = res + roman_numeral[j]
            elif result ==4:
                res = res + roman_numeral[j]+ roman_numeral[j-1]
            elif result > 4 and result < 9:
                res = res + roman_numeral[j-1]
                for z in range(5,result):
                    res = res + roman_numeral[j]
            elif result==9:
                res = res + roman_numeral[j] + roman_numeral[j-2]
            num = num % integer[j]
        return res
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值