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. skip-grant-tables

    1.net stop mysql 2.my.ini中[mysqld]plugin_dir的下面增加skip-grant-tables 3.net start mysql 4.在Navicat中打开my ...

  2. 版本控制SVN与Eclipse4.4.1集成 ( eclipse svn:E175002错误解决 )

    Eclipse版本--Luna Service Release 1(4.4.1) SVN版本-----1.8.X 系统OS-------ubuntu 14.04 LTS 1.通过地址安装 Help-& ...

  3. js基础知识之_入门变量和运算符

    js页面效果学习 (轮播图,文字滚动效果等等) javascript能来做什么 1.数据验证 2.将动态的内容写入网页中(ajax) 3.可以对时间做出响应 4.可以读写html中的内容 5.可以检测 ...

  4. 使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比

    今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章,自己添加了一些内容,做了简单的总结,这个语句还是第一次见到,学习了.我从简单到复杂地写, ...

  5. 使用sprintf打印float并控制小数位数时引起的问题

    最近在做项目中发现一个Bug,直接把进程搞死,查了一下,居然是一个最不起眼的地方导致的,在此记录一下. 先看下面代码 #include <iostream> #include <st ...

  6. Savelog项目总结回忆

    Savelog项目的细节已经不太记得,感觉有些遥远,需要翻回旧的笔记本电脑或者是旧的笔记本. 概述: 本项目采用的Linux C,监听一个或多个特殊的端口,当其中一个端口有发起连接时就产生一个新的线程 ...

  7. DateTimePicker时间控件:

    DateTimePicker时间控件: http://xdsoft.net/jqplugins/datetimepicker/ 可以参考文档设置各种属性,格式. 用法: 首先下载datetimepic ...

  8. 关于移动div的具体实现(js)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 使用css3画饼图

    CSS3 Gradient介绍参考地址: http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-linear-gradient.html http: ...

  10. ecshop安装程序源码阅读-安装脚本(1)

    定义系统判断常量 引入安装初始化文件 设置时区 报告所有错误 定义站点根常量 定义php自身相对路径 引入系统,公共函数库 引入并初始化错误处理对象 引入并初始化模板引擎 引入安装相关业务 发送HTT ...