[LeetCode#12] Roman to Integer
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的更多相关文章
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- 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 ,即 ...
- 【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 ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- [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 ...
- 【leetcode】Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
随机推荐
- CentOS 6.7安装配置Ansible
1.准备CentOS环境 yum update && yum upgrade 2.控制服务器与被管理服务器要求 Master:Python 2.6+ Slave:Python 2.4+ ...
- JS1 js获取dom元素方法
js获取dom元素方法 1.通过ID选取元素(getElementById) 1)使用方法:document.getElementById("domId") 其 ...
- (转)META http-equiv="refresh" 实现网页自动跳转
***.html自动跳转文件代码如下: <HTML> <HEAD><META http-equiv="Refresh" content="5 ...
- double 类型运算会出现精度问题
要先转换为字符串,后进行运算,可以写个方法做乘法运算public static double mul(double v1,double v2){BigDecimal b1 = new BigDecim ...
- 吐槽:Lambda表达式
前面我曾经讨论过Lambda表达式(也就是匿名表达式)的用法, 这里我就主要强调一下匿名表达式的好处. 首先是不需要写多余的方法体,特别是订阅事件的时候,但是也有一个问题,那就是单个方法会因为匿名表达 ...
- LINQ 101——约束、投影、排序
什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询.集合以及转换操作.它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能 ...
- Java RMI 框架_远程方法调用(2016-08-16)
概念: Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可 ...
- 忘记Mysql的root密码怎么办?
解决方法: 1.打开cmd,用net start命令查看是否开启了mysql服务,如果开启,用net stop mysql 命令关闭mysql 2.进入mysql的安装目录下的bin目录,例如:E:\ ...
- oracle查看用户信息
1.查看所有用户:select * from dba_users; select * from all_users; select * from user_users;2.查看用户或角色系统权限(直接 ...
- Island of Survival 概率
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...