Roman numerals are represented by seven different symbols: IVXLCD and M.

  1. Symbol Value
  2. I 1
  3. V 5
  4. X 10
  5. L 50
  6. C 100
  7. D 500
  8. 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.

Example 1:

  1. Input: 3
  2. Output: "III"

Example 2:

  1. Input: 4
  2. Output: "IV"

Example 3:

  1. Input: 9
  2. Output: "IX"

Example 4:

  1. Input: 58
  2. Output: "LVIII"
  3. Explanation: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

  1. Input: 1994
  2. Output: "MCMXCIV"
  3. Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

将阿拉伯整数转为罗马数字,首先要对罗马数字有了解,找到两种数字转换的规律,然后用一个Hash map来保存这些规律,然后把整数进行相应的转换。输入数字的范围(1 - 3999)。

Java:

  1. public static String intToRoman(int num) {
  2. String M[] = {"", "M", "MM", "MMM"};
  3. String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
  4. String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
  5. String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
  6. return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
  7. }  

Java:

  1. public class Solution {
  2. public String intToRoman(int number) {
  3. int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
  4. String[] numerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
  5. StringBuilder result = new StringBuilder();
  6. for (int i = 0; i < values.length; i++) {
  7. while (number >= values[i]) {
  8. number -= values[i];
  9. result.append(numerals[i]);
  10. }
  11. }
  12. return result.toString();
  13. }
  14. } 

Python:

  1. class Solution(object):
  2. def intToRoman(self, num):
  3. """
  4. :type num: int
  5. :rtype: str
  6. """
  7. numeral_map = {1: "I", 4: "IV", 5: "V", 9: "IX", \
  8. 10: "X", 40: "XL", 50: "L", 90: "XC", \
  9. 100: "C", 400: "CD", 500: "D", 900: "CM", \
  10. 1000: "M"}
  11. keyset, result = sorted(numeral_map.keys()), []
  12.  
  13. while num > 0:
  14. for key in reversed(keyset):
  15. while num / key > 0:
  16. num -= key
  17. result += numeral_map[key]
  18.  
  19. return "".join(result)  

Python:

  1. strs = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
  2. nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
  3. ret = ""
  4. for i, j in enumerate(nums):
  5. while num >= j:
  6. ret += strs[i]
  7. num -= j
  8. if num == 0:
  9. return ret

Python:

  1. def intToRoman1(self, num):
  2. values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]
  3. numerals = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ]
  4. res, i = "", 0
  5. while num:
  6. res += (num//values[i]) * numerals[i]
  7. num %= values[i]
  8. i += 1
  9. return res

Python:  

  1. def intToRoman(self, num):
  2. values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]
  3. numerals = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ]
  4. res = ""
  5. for i, v in enumerate(values):
  6. res += (num//v) * numerals[i]
  7. num %= v
  8. return res  

C++:

  1. class Solution {
  2. public:
  3. string intToRoman(int num) {
  4. const vector<int> nums{1000, 900, 500, 400, 100, 90,
  5. 50, 40, 10, 9, 5, 4, 1};
  6. const vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L",
  7. "XL", "X", "IX", "V", "IV", "I"};
  8.  
  9. string result;
  10. int i = 0;
  11. while (num > 0) {
  12. int times = num / nums[i];
  13. while (times--) {
  14. num -= nums[i];
  15. result.append(romans[i]);
  16. }
  17. ++i;
  18. }
  19. return result;
  20. }
  21. };

  

类似题目:

[LeetCode] 13. Roman to Integer 罗马数字转为整数  

[LeetCode] 12. Integer to Roman 整数转为罗马数字的更多相关文章

  1. [leetcode]12. Integer to Roman整数转罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  2. 【LeetCode】Integer to Roman(整数转罗马数字)

    这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...

  3. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  4. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  5. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

  6. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  7. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  8. lintcode :Integer to Roman 整数转罗马数字

    题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...

  9. Leetcode12.Integer to Roman整数转罗马数字

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

随机推荐

  1. 数据库迁移Flyway

    为什么需要Flyway 日常开发常常会遇到一些这样的场景 小红开发一个模块在本地数据库增加了两个字段,并且改动了dao层的代码提交到git.这时候小黄拉取了代码Run很可能报错. 如果在上线正式环境的 ...

  2. linux Crontab定时备份项目案例

    首先先写好备份的脚本(拷贝的命令) #bash/bin cd /finance/tomcat8-finance/wtpwebapps tar -czf /finance/webapp_backup/* ...

  3. springboot+mybatisplus进行整合并且使用逆向工程

    首先引入maven依赖:这是整合mybatisplus时,进行逆向工程时候需要引入的依赖 <!--mybaitsplus start--> <dependency> <g ...

  4. CodeForces - 83D:Numbers (数学&递归 - min25筛 )

    pro:给定三个整数L,R,P求[L,R]区间的整数有多少个是以P为最小因子的.L,R,P<2e9; sol: 一: 比较快的做法是,用函数的思想递归. 用solve(N,P)表示求1到N有多少 ...

  5. BZOJ3028 食物 和 LOJ6261 一个人的高三楼

    总结一下广义二项式定理. 食物 明明这次又要出去旅游了,和上次不同的是,他这次要去宇宙探险!我们暂且不讨论他有多么NC,他又幻想了他应该带一些什么东西.理所当然的,你当然要帮他计算携带N件物品的方案数 ...

  6. python获取参数列表

    def f(a=1, b=2, c=3): print(locals())#在函数内获取 #使用inspect模块,简单方便 python2.7: import inspectinspect.geta ...

  7. 用python做数据分析pandas库介绍之DataFrame基本操作

    怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 这一部分主要学习pandas中基于前面两种数据结构的基本操作. 设有DataF ...

  8. pandas 6 时间

    类 备注 创建方法 Timestamp 时刻数据 to_datetime,Timestamp DatetimeIndex Timestamp的索引 to_datetime,date_range,Dat ...

  9. ubuntu18.04 + python3 安装pip3

    最近在学习python 网络爬虫,正好接触到python的requests模块 我的开发环境是ubuntu18.04+python3,这个系统是默认自带了python3,且版本是python 3.6. ...

  10. PDB文件会影响性能吗?

    有人问了这样的问题:"我工作的公司正极力反对用生成的调试信息构建发布模式二进制文件,这也是我注册该类的原因之一.他们担心演示会受到影响.我的问题是,在发布模式下生成符号的最佳命令行参数是什么 ...