首页 > 编程语言 > 详细

[LeetCode][JavaScript]Integer to Roman

时间:2015-08-30 17:22:19      阅读:242      评论:0      收藏:0      [点我收藏+]

Integer to Roman

Given an integer, convert it to a roman numeral.

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

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

 

 


 

 

阿拉伯数字转罗马数字。

造表,把有所的基数都放到表中,主要是要放入IV,IX这类数,方便处理。

从大到小匹配表中的数,一轮循环搞定。

 1 /**
 2  * @param {number} num
 3  * @return {string}
 4  */
 5 var intToRoman = function(num) {
 6     var number = [1,   4,    5,   9,    10,  40,   50,  90,   100, 400,  500, 900,  1000];
 7     var roman =  ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];
 8     var tmp, index = 12, res = ""; 
 9     while(index >= 0){
10         if(number[index] > num){
11             index--;
12             continue;
13         }
14         while(num >= number[index]){
15             num -= number[index];
16             res += roman[index];
17         }
18         index--;
19     }
20     return res;
21 };

技术分享

[LeetCode][JavaScript]Integer to Roman

原文:http://www.cnblogs.com/Liok3187/p/4771082.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!