[leetcode]_Integer to Roman
题目:对应之前那道将罗马数字转换整型数字的题目。反过来。
思路:刚开始做的时候,想着用程序进行判断,复杂的要死。网络了别人代码,非常清晰。
代码:
1 public String intToRoman(int num) {
String[] alpha = {"M" ,"CM" , "D" , "CD" , "C" ,"XC" , "L" , "XL" , "X" ,"IX" , "V" , "IV" , "I"};
int[] value = new int[]{1000 ,900 , 500 , 400 , 100 , 90 , 50 , 40 ,10 , 9 , 5 , 4 , 1};
String result = new String();
for(int i = 0 ; num != 0 ; i++){
while(num >= value[i]){
num -= value[i];
result += alpha[i];
}
}
return result;
}
用贪心的思路,直接AC,very nice~
[leetcode]_Integer to Roman的更多相关文章
- leetcode第一刷_Integer to Roman
这道题当时不会写,是參照discuss写的. 首先要弄明确罗马数字的规则,这个在国外难道是常识吗.为什么题干一点都没讲.. 4000以下一共同拥有以下几种符号:"M", " ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【一天一道LeetCode】#13. Roman to Integer
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
- 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
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...
- Leetcode Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【leetcode】13. Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...
- 【算法】LeetCode算法题-Roman To Integer
这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...
随机推荐
- PostgreSQL在Ubuntu上安装指南
安装环境: Ubuntu 10.04-desktop-i386 PostgreSQL 8.4 1. 安装PostgreSQL 输入如下命令 sudo apt-get install postgresq ...
- [POJ 1385] Lifting the Stone (计算几何)
题目链接:http://poj.org/problem?id=1385 题目大意:给你一个多边形的点,求重心. 首先,三角形的重心: ( (x1+x2+x3)/3 , (y1+y2+y3)/3 ) 然 ...
- 配置Tomcat数据源
1.方式一:在server.xml中配置 1)tomcat安装路径下conf目录下的server.xml,在<GlobalNamingResources>和</GlobalNamin ...
- elasticsearch从mysql导入数据
详细:https://github.com/jprante/elasticsearch-jdbc(最下面有各数据库的导入方法说明) elasticsearch版本为1.5.2 1.下载 elastic ...
- (转) C# textbox 限制输入问题
原理:e.handled代表这次按键动作是否交由用户自己处理,如果为true代表由用户处理,系统不再过问,这里的应用是拦截,即通知系统我要处理这个数据,但又不处理(丢弃数据),从而实现拦截的效果. 在 ...
- Arch xfce4 安装解压缩软件
>>>安装方法 pacman -S thunar-archive-plugin xarchiver zip unzip p7zip arj lzop cpio unrar >& ...
- js 未结束的字符串常量错误解决方法
1.JAVASCRIPT引用时,使用的字符语言不一致. 比如:<script type=”text/javascript” src=”xxx.js” charset=”UTF-8″>.xx ...
- (续篇3):飞测独家のJmeter秘籍,限量发放
好东西,分享大家,自上次分享出来fiddler导出jmx格式V4.0版本对外公开后,收到一些反馈,我们利用工作之余时间继续优化,现在一个比较稳定的版本出炉,分享给大伙,我们一起来看看. 特性说明: 版 ...
- CCJ PRML Study Note - Chapter 1.6 : Information Theory
Chapter 1.6 : Information Theory Chapter 1.6 : Information Theory Christopher M. Bishop, PRML, C ...
- GLib基础
实用功能 GLib中包含了近二十种实用功能,从简单的字符处理到初学者很难理解的XML解析功能,这里介绍两种较简单的:随机数和计时. 下面代码演示如何产生1-100之间的随机整数和演示如何计算30000 ...