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. Powerdesigner中如何生成测试数据

    设计表完成以后,我们需要生成一些测试数据,可以直接更新到数据库中,下面我们就来试试: 第一步:建立需要的Profiles测试文件,[Model]--[Test Data Profiles],如图所示: ...

  2. JavaScript入门(1)

    一.JS基本 1.JS代码位置 <script type="text/javascript">表示: <script></script>之间是文 ...

  3. HTML+CSS基础学习笔记(8)

    一.水平居中设置--行内元素 如果设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置text-align:center来实现的 二.水平居中设置 --定宽块状元素 #当被设置元素为块状元素时 ...

  4. html不同文档类型支持的元素标签

  5. Spring Framework jar官方直接下载路径

    SPRING官方网站改版后,建议都是通过 Maven和Gradle下载,对不使用Maven和Gradle开发项目的,下载就非常麻烦,下给出Spring Framework jar官方直接下载路径: h ...

  6. 1. 连接字符串的创建 - Lazy.Framework从零开始设计自己的ORM架构

    开发初衷 注册了博客园已经有几个月了,却从来都没有上来过,本人大概从2010年开始就开始做.NET 方向的开发. 这个是我在博客园发布的第一个帖子. 主要就是说说最近在写的一个ORM架构. 本人接触的 ...

  7. oracle插入数据报错ORA-26026

    今天进行数据清理时发现报错ORA-26026,主要是把从交易库提取数据并插入到归档库中. 检查一下发现是归档库的索引问题. 当时为了提高插入速度,所以删除了归档库的索引,可能对主键索引产生了影响. 解 ...

  8. xmlns:tools="http://schemas.android.com/tools"以及tools:context=".ConfActivity"是什么意思

    xmlns:tools="http://schemas.android.com/tools"这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么 ...

  9. 《React-Native系列》38、 ReactNative混合组件封装

    在我们做ReactNative项目的过程中,我们会发现由ReactNative提供给我们的组件或API往往满足不了我们的需求,常常需要我们自己去封装Native组件. 今天我们介绍下如果封装一个简单的 ...

  10. C# Winform开发框架模块图(平台核心模块+示例模块)

    企业版V4.0 - 模块图   企业版V4.0 - 项目解决方案   Client/Server构架,有两个解决方案:     客户端解决方案说明:     服务端解决方案说明: C/S系统开发框架- ...