[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,下面这道题目就和罗马数 ...
随机推荐
- ASPxGridView中DetailRow的使用
ASPxGridView是一个方便的数据显示控件,可是自动的绑定我们所需要的数据,但是有时,当数据属性过多时,我们并不一定要把所有的信息提供给所有的人,当有人需要这些数据时可以自动的进行查看,这时就可 ...
- Delphi 连接mysql 的功能, 去除乱码, 需要设置字符集
vDataBaseName := aConfiginiFile.ReadString('DataBaseConfig', 'DataBase', CH_IPC712Db); vServer := aC ...
- [SQL]不知道
--COALESCE --返回其参数中第一个非空表达式. --语法 --COALESCE ( expression [ ,...n ] ) --参数 --expression --任何类型的表达式. ...
- [SQL]SQL类似统计功能的sql文
declare @t table(name varchar(),type int) insert into @t union all union all union all union all if ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- 页面设计--Tree目录树
Tree目录树控件属性: 根据数据集合来配置相应的信息 加载模式有自动加载.自定加载 web中显示效果图:
- java 对象类型的转换
import com.java.charpt05.NewStr; class Quadrangle{ public static void draw(Quadrangle q) { ...
- & replace &
var decoded = encoded.replace(/&/g,'&'); http://stackoverflow.com/questions/3700326/decode-a ...
- Android开发-API指南-<manifest>
<manifest> 英文原文:http://developer.android.com/guide/topics/manifest/manifest-element.html 采集(更新 ...