[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,下面这道题目就和罗马数 ...
随机推荐
- ArcGIS Server建立缓存(切图)原理解析[图解] (转载)
GoogleMap ,VirtualEarth ,YahooMap 等,目前所有的WebGIS都使用了缓存机制 以提高地图访问速度.原理都是将地图设定为多个比例尺,对于每个比例尺提前将地图分成若干小图 ...
- android SFC
本系列适合0基础的人员,因为我就是从0开始的,此系列记录我步入Android开发的一些经验分享,望与君共勉!作为Android队伍中的一个新人的我,如果有什么不对的地方,还望不吝赐教. 在开始Andr ...
- (easy)LeetCode 231.Power of Two
Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...
- [POJ 1385] Lifting the Stone (计算几何)
题目链接:http://poj.org/problem?id=1385 题目大意:给你一个多边形的点,求重心. 首先,三角形的重心: ( (x1+x2+x3)/3 , (y1+y2+y3)/3 ) 然 ...
- xcode C++一些简单设置
下面是一个要用到mysql库的C++程序设置: 添加用户头文件: 双击项目-Build Settings-Search Paths: Library Search Paths: /usr/local/ ...
- HRBUST1530
链接 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1530 这个是典型的二分题,题 ...
- canvas-star6.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL 密码修改
方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...
- 解决setInterval计时器不准的问题
在js中如果打算使用setInterval进行倒数,计时等功能,往往是不准确的,因为setInterval的回调函数并不是到时后立即执行,而是等系统计算资源空闲下来后才会执行.而下一次触发时间则是在s ...
- HTTP协议——学习资料小结
嗯,这几天回头再次的学习Servlet的知识点,觉得HTTP协议的内容是相当重要的,现在虽然知道浏览器与应用程序的交互离不开它,但是怎么将信息从浏览器传输到服务器的这个知识点还是一个盲点.于是从网上找 ...