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. C# 以共享只读方式打开被其它程序占用的文件

    iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, Sys ...

  2. Linux 下安装FastDFS v5.08 的php扩展

    php扩展也需要依赖于FastDFS一些库文件,所以请先安装FastDFS,具体请看我之前的文章.   一.安装目录 php安装目录 /data/nmp/php FastDFS源码目录 /data/w ...

  3. goaccess 通过jsonpath 转换为prometheus metrics

    goaccess 是一个不错的日志分析工具,包含了json 数据同时支持基于websocket 的实时数据处理,当然我们可以通过jsonpath 的exporter 转换为支持promethues 的 ...

  4. exactly-once和kafka

    Exactly-Once的概念是指"恰好一次",简单讲就是同一个数据只会被处理一次,应用有机质保证不会重复处理同一条数据(如果数据因为因为网络业务异常被发送多次):Exactly- ...

  5. httpd基础知识

    apache简介   Apache取自"a patchy server"的读音,意思是充满补丁的服务器.Apache起初由伊利诺伊大学香槟分校的国家超级电脑应用中心(NCSA)开发 ...

  6. 平均数_中位数_众数在SqlServer实现

    平均数.中位数.众数都是度量一组数据集中趋势的统计量.所谓集中趋势是指一组数据向某一中心值靠拢的倾向,测度集中趋势就是寻找数据一般水平的代表值或中心值.而这三个特征数又各有特点,能够从不同的角度提供信 ...

  7. 一些有用的Java学习资料

    Better Java,一些好的Java实践 Google Java Style Guide 30个Java编程技巧 JDK8新增语法特性简介,对Java8中新增的函数接口.Lambda表达式.方法引 ...

  8. bananapi+OLED做的一个打地鼠游戏,c语言编程

    说明一下:BPI是对拍死的BPI的计数,对应最终的成绩RANK是难度 数值越低难度越高 每当打死10个BPI以后就会减一即难度高一级 默认初始化RANK等于15 DIE是存在的BPI数量,一旦数量大于 ...

  9. IIS 添加 MIME

    参考:https://blog.brain1981.com/727.html 在项目的  Web.Config 里下添加如下段落即可: <?xml version="1.0" ...

  10. ZooKeeper和CAP理论及一致性原则

    一.CAP理论概述CAP理论告诉我们,一个分布式系统不可能同时满足以下三种 一致性(C:Consistency)可用性(A:Available)分区容错性(P:Partition Tolerance) ...