Problem:

Given an integer, convert it to a roman numeral.

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

Analysis:

This problem is trivial, if you really understand the principle to construct a Roman number.
It is really really very very simple!
Note: Keep in mind!!! The character used in roman number include digit weight, but for numerial system it based on position. Thus you should find a way to convert the digit weight(in positional representation) into digital weight(through character combination) for (int i = 0; i < 4; i++) {
int digit_weight = (int)Math.pow(10, 3-i);
int digit = num / digit_weight;
switch (digit) {
...
}
}

Solution:

public class Solution {
public String intToRoman(int num) {
if (num <= 0 || num > 3999)
throw new IllegalArgumentException("The passed in argument is illegal");
StringBuffer ret = new StringBuffer();
HashMap<Integer, Character> map = new HashMap<Integer, Character> ();
map.put(1, 'I');
map.put(5, 'V');
map.put(10, 'X');
map.put(50, 'L');
map.put(100, 'C');
map.put(500, 'D');
map.put(1000, 'M');
for (int i = 0; i < 4; i++) {
int digit_weight = (int)Math.pow(10, 3-i);
int digit = num / digit_weight;
switch (digit) {
case 1 :
ret.append(map.get(digit_weight));
break;
case 2 :
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight));
break;
case 3:
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight));
break;
case 4:
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight * 5));
break;
case 5:
ret.append(map.get(digit_weight * 5));
break;
case 6:
ret.append(map.get(digit_weight * 5));
ret.append(map.get(digit_weight));
break;
case 7:
ret.append(map.get(digit_weight * 5));
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight));
break;
case 8:
ret.append(map.get(digit_weight * 5));
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight));
break;
case 9:
ret.append(map.get(digit_weight));
ret.append(map.get(digit_weight * 10));
break;
}
num = num % digit_weight;
}
return ret.toString();
}
}

[LeetCode#12] Roman to Integer的更多相关文章

  1. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

  2. Leetcode#13. Roman to Integer(罗马数字转整数)

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

  3. 【LeetCode】Roman to Integer & Integer to Roman

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

  4. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  5. Leetcode 13. Roman to Integer(水)

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

  6. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  7. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

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

  8. 【leetcode】Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  9. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

随机推荐

  1. HTML 5 全局属性

    下面的全局属性可用于任何 HTML5 元素.HTML 5 全局属性NEW:HTML 5 中新的全局属性.属性 描述accesskey 规定访问元素的键盘快捷键class   规定元素的类名(用于规定样 ...

  2. display: inline-block 的神奇效果

    先上要实现的效果图: 方案一:来自卢兄: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  3. js--小结⑤

    js中的for循环,while循环,do...while循环和C语言的一模一样 有几个问题要提醒一下的是 1.  null是对象,即object       undefined是undefined d ...

  4. Java写一个简单学生管理系统

    其实作为一名Java的程序猿,无论你是初学也好,大神也罢,学生管理系统一直都是一个非常好的例子,初学者主要是用数组.List等等来写出一个简易的学生管理系统,二.牛逼一点的大神则用数据库+swing来 ...

  5. 浅谈.net中的params关键字

    先举个例子: 代码如下: class Program { static void Main(string[] args) { Console.WriteLine(Sum(1)); Console.Wr ...

  6. 基于shiro授权过程

    1.对subject进行授权,调用方法isPermitted("permission串")2.SecurityManager执行授权,通过ModularRealmAuthorize ...

  7. iOS设备、Icon、LaunchImage、图片分辨率

    iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...

  8. LINQ 101——分组、Set、转换、Element

    一.Grouping(分组) 例1:对于0-9数按被3整除的结果分组 代码: static void Linq1() { , , , , , , , , , }; var numModBy3 = fr ...

  9. 为什么用linear regression可以做classification

    输出空间 错误衡量方式 能不能直接用linear regression for classification 当成一个分类器回传回去 heuristic(启发式的:试探) 错误衡量 complexit ...

  10. Qt5下的常见问题————C1083

    很多像我一样刚开始学习Qt的时候都会遇到这样的问题.例如"fatal error C1083: 无法打开包括文件:“QApplication”: No such file or direct ...