leetcode:Integer to Roman
来源:互联网 发布:软件开发入门培训 编辑:程序博客网 时间:2024/05/02 16:13
把1-3999的数字转化为罗马数字
具体转换方法,详细请百度,
直接贴代码如下:
public class Solution { static HashMap<Integer, String> map = new HashMap<>(47); public Solution(){ map.clear(); this.put(1, "I").put(2, "II").put(3, "III").put(4, "IV").put(5, "V").put(6, "VI").put(7, "VII") .put(8, "VIII").put(9, "IX").put(10, "X").put(20, "XX").put(30, "XXX").put(40, "XL") .put(50, "L").put(60, "LX").put(70, "LXX").put(80, "LXXX").put(90, "XC").put(100, "C") .put(200, "CC").put(300, "CCC").put(400, "CD").put(500, "D").put(600, "DC").put(700, "DCC") .put(800, "DCCC").put(900, "CM").put(1000, "M").put(2000, "MM").put(3000, "MMM"); this.put(0, ""); } public String intToRoman(int num) { int mod = 1000; StringBuilder sb = new StringBuilder(30); while(num != 0 && mod != 0){ sb.append(get(num / mod * mod)); num %= mod; mod /= 10; } return sb.toString(); } private Solution put(Integer n, String str){ map.put(n, str); return this; } private String get(int n){ return map.get(n); } }
0 0
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- LeetCode Integer to Roman & Roman to Integer
- leetcode - roman to integer & integer to roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- LeetCode: Integer to Roman
- LeetCode : Roman to Integer
- [Leetcode] Roman to Integer
- [Leetcode] Integer to Roman
- Leetcode: Integer to Roman
- Leetcode: Roman to Integer
- LeetCode Integer to Roman
- 【leetcode】Roman to Integer
- 输出一组数中的次大值
- [LeetCode] Insertion Sort List
- 51nod-1082 . 与7无关的数
- Android开发错误——Could not find class
- 一个简单的数据增量更新策略(Android / MongoDB / Django)
- leetcode:Integer to Roman
- 享年百歲
- 说说静态URL与动态URL
- linux下core文件调试方法
- System类
- AngularJS ng-repeat嵌套循环中,如何取得外层循环的索引值
- 国家集训队论文分类
- 小贝学习-xml
- 单链表的游标实现:插入、删除、查找、打印等功能