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. Pivotal Cloud Foundry学习笔记(1)

    PCF是一个PAAS平台 注册PCF账号 https://account.run.pivotal.io/sign-up 安装cf CLI 访问 https://console.run.pivotal. ...

  2. windows系统 安装MongoDB 32位

    本篇文章记录了我在win7 32位下安装MongoDB的步骤,以作记录. 下文的安装方法参考了以下博文: http://www.cnblogs.com/lzrabbit/p/3682510.html ...

  3. Handler 原理分析和使用(一)

    我为什么写Handler,原因主要还在于它在整个 Android 应用层面非常之关键,他是线程间相互通信的主要手段.最为常用的是其他线程通过Handler向主线程发送消息,更新主线程UI. 下面是一个 ...

  4. asp.net <%%> <%#%><%=%><%@%><%$%>用法与区别

    1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...

  5. Win7使用IIS通过域名访问本地程序(网页、css、js等)

    一.目的:在本地浏览器里面,输入www.abc.com 可以访问我们本地搭建的网页程序 二.好处:在本地模拟,真实的访问,另外可以设置一些二级域名,例如static.abc.com域名用来存储像图片, ...

  6. SQL反模式部分内容笔记

    规范化: 1, 以一种我们能够理解的方式表达这个世界中的事物; 2, 减少数据冗余存储, 防止异常或者不一致的数据; 3, 支持完整性约束.  Tips: 提高数据的性能不在此列表中. 意义: 规范化 ...

  7. Javascript字符串拼接小技巧

    在Javascript中经常会遇到字符串的问题,但是如果要拼接的字符串过长就比较麻烦了. 如果是在一行的,可读性差不说,如果要换行的,会直接报错. 在此介绍几种Javascript拼接字符串的技巧. ...

  8. Java小例子(学习整理)-----学生管理系统-控制台版

    1.功能介绍: 首先,这个小案例没有使用数据库,用集合的形式暂时保存数据,做测试! 功能: 增加学生信息 删除学生信息 修改学生信息 查询学生信息:  按照学号(精确查询)  按照姓名(模糊查询) 打 ...

  9. 求两个数的最大公约数(Euclid算法)

    求两个数 p 和 q 的最大公约数(greatest common divisor,gcd),利用性质 如果 p > q, p 和 q 的最大公约数 = q 和 (p % q)的最大公约数. 证 ...

  10. 关于【键鼠<局域网>共享软件:synergy】install

    Installation 另外,本人在centos6.5环境下作为server运行时,遇到一个问题,synergy1.5随着系统升级居然变成了1.3X,所以如果遇到类似问题,请您先用 rpm -qa ...