Roman numerals are represented by seven different symbols: IVXLCD 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.

Example 1:

Input: 3
Output: "III"

Example 2:

Input: 4
Output: "IV"

Example 3:

Input: 9
Output: "IX"

Example 4:

Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:

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

Solution1: Simulation

Let's walk through an example,  if input: 6,  output is  "LI",  we find the maximum value that we can remove from 6,  iteratively doing that and appending its corresponding Roman numerals into result.

Step1,  For most cases, Roman Numeral use addition(相加).

Coz only a few cases using subtraction(相减) like 4 or 9,  we can pre-calculate those ones.

   int values[] = {1000, 900,  500, 400, 100,  90,  50,  40,   10,   9,   5,    4,     1};
String romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

Step2, traverse the values[],  find the maximum value we can remove from 6

   int values[] = {1000, 900,  500, 400, 100,  90,  50,  40,   10,   9,   5,    4,     1};
String romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

Step3, append its corresponding Roman numerals into result, remove it from 6, and jump to Step2 to do the next iteration

code:

 /*
Time Complexity: O(n)
Space Complexity: O(1)
*/
class Solution {
public String intToRoman(int num) {
final int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
final String romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.length; i++){
while( num >= values[i]){
num -= values[i];
sb.append(romans[i]);
}
}
return sb.toString();
}
}

[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] 12. Integer to Roman 整数转化成罗马数字

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

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

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

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

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

  5. Leetcode 12——Integer to Roman

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

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

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

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

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

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

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

  9. [LeetCode] 12. Integer to Roman ☆☆

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. PythonStudy1——Python 值拷贝 浅拷贝 深拷贝

    拷贝:对值进行复制的过程 # 值拷贝:应用场景最多  ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来  # ls内部的值发生任何变化,ls1都会随 ...

  2. 第三章 JQuery: HelloWorld--常见方法--css--选择器--筛选器--属性--效果--事件--数组操作--字符串操作--对象转换

    1.jQuery简介 为了简化JavaScript 的开发, 一些JavsScript 库诞生了. JavaScript库封装了很多预定义的对象和实用函数.能帮助使用者建立有高难度交互的页面, 并且兼 ...

  3. WebForm AnyWay

    项目地址 :  https://github.com/kelin-xycs/WebFormAnyWay WebForm AnyWay 用 WebForm 构建 简洁高效 的 Web 应用 WebFor ...

  4. a small notepad++ plugin to support doxygen 1key generate

    Precondition: doxygen in c:\ folder testdoxygen.bat --- path %path%;C:\Doxygen;C:\Doxygen\graphviz\b ...

  5. c# 仿微信二维码生成

    /// <summary> /// 生成二维码. /// </summary> /// <param name="data">需要添加进去的文本 ...

  6. JAVA 集合操作总结

    1.Collection 1.基本操作 对集合的基础操作 1.boolean add(Object o) //添加对象到集合 2.boolean remove(Object o) //删除指定的对象 ...

  7. Python3 字典 setdefault() 方法

    Python 字典 setdefault() 函数和get() 方法类似, 如果键不存在于字典中,将会添加键并将值设为默认值. dict.setdefault(key, default=None) k ...

  8. Python图形开发之PIL

    1.背景介绍 PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 2.安装 Windows平台:PIL官 ...

  9. Kong管理UI -kong-dashboard

    本文仍然是在ubuntu18的环境下进行 https://github.com/PGBI/kong-dashboard kong dashboart如果要正常使用管理UI,前提为kong已经正常run ...

  10. [sharepoint]Rest api相关知识(转)

    写在前面 最近又开始弄rest api了,通过sharepoint rest api获取站点信息,Items,fields非常方便,再结合OData查询,更是得心应手.这里记录学习的时候用到的知识点, ...