题目:

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

链接:https://leetcode.com/problems/integer-to-roman/#/description

4/9/2017

18%, 115ms

 1 public class Solution {
 2     public String intToRoman(int num) {
 3         HashMap<Integer, Character> hm = new HashMap<Integer, Character>();
 4         StringBuilder sb = new StringBuilder();
 5         hm.put(1, 'I');
 6         hm.put(5, 'V');
 7         hm.put(10, 'X');
 8         hm.put(50, 'L');
 9         hm.put(100, 'C');
10         hm.put(500, 'D');
11         hm.put(1000, 'M');
12
13         while (num > 0) {
14             if (num / 1000 != 0) {
15                 int count = num / 1000;
16                 num -= 1000 * count;
17                 while (count > 0) {
18                     sb.append(hm.get(1000));
19                     count--;
20                 }
21             } else if (num / 900 != 0) {
22                 sb.append(hm.get(100));
23                 sb.append(hm.get(1000));
24                 num -= 900;
25             } else if (num / 500 != 0) {
26                 sb.append(hm.get(500));
27                 num -= 500;
28             } else if (num / 400 != 0) {
29                 sb.append(hm.get(100));
30                 sb.append(hm.get(500));
31                 num -= 400;
32             } else if (num / 100 != 0) {
33                 int count = num / 100;
34                 num -= 100 * count;
35                 while (count > 0) {
36                     sb.append(hm.get(100));
37                     count--;
38                 }
39             } else if (num / 90 != 0) {
40                 sb.append(hm.get(10));
41                 sb.append(hm.get(100));
42                 num -= 90;
43             } else if (num / 50 != 0) {
44                 sb.append(hm.get(50));
45                 num -= 50;
46             } else if (num / 40 != 0) {
47                 sb.append(hm.get(10));
48                 sb.append(hm.get(50));
49                 num -= 40;
50             } else if (num / 10 != 0) {
51                 int count = num / 10;
52                 num -= 10 * count;
53                 while (count > 0) {
54                     sb.append(hm.get(10));
55                     count--;
56                 }
57             } else if (num / 9 != 0) {
58                 sb.append(hm.get(1));
59                 sb.append(hm.get(10));
60                 num -= 9;
61             } else if (num / 5 != 0) {
62                 sb.append(hm.get(5));
63                 num -= 5;
64             } else if (num / 4 != 0) {
65                 sb.append(hm.get(1));
66                 sb.append(hm.get(5));
67                 num -= 4;
68             } else {
69                 while (num > 0) {
70                     sb.append(hm.get(1));
71                     num--;
72                 }
73             }
74         }
75         return sb.toString();
76     }
77 }

其实可以多列出来很多case,比如9,4的情况。甚至可以列出来所有的例子

别人的答案:

 1 public class Solution {
 2     public String intToRoman(int num) {   //Roman:   I = 1,  V = 5,   X = 10,   L = 50,   C = 100,  D = 500,  M = 1000
 3         StringBuilder result = new StringBuilder();
 4         String [] symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
 5         int [] value = {1000,900,500,400, 100, 90,  50, 40,  10, 9,   5,  4,   1};
 6         for(int i = 0; num != 0; i++){
 7             while(num >= value[i]){
 8                 num -= value[i];
 9                 result.append(symbol[i]);
10             }
11         }
12         return result.toString();
13     }
14 }

https://discuss.leetcode.com/topic/12384/simple-solution

1 public static String intToRoman(int num) {
2     String M[] = {"", "M", "MM", "MMM"};
3     String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
4     String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
5     String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
6     return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
7 }

更多讨论:https://discuss.leetcode.com/category/20/integer-to-roman

转载于:https://www.cnblogs.com/panini/p/6687283.html

12. Integer to Roman相关推荐

  1. LeetCode 12. Integer to Roman

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  2. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  3. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", &q ...

  4. leetcode 12 ,13 Integer to Roman amp;amp;Roman to Integer 罗马与阿拉伯数组转换

    12 Integer to Roman 13 Roman to Integer 有可能不注意的结果: class Solution {public:/*1.相同的数字连写,所表示的数等于这些数字相加得 ...

  5. lintcode :Integer to Roman 整数转罗马数字

    题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...

  6. Integer to Roman 问题

    Integer to Roman 问题 leetcode java 1.问题描述 Given an integer, convert it to a roman numeral.Input is gu ...

  7. LeetCode算法入门- Roman to Integer Integer to Roman -day8

    LeetCode算法入门- Roman to Integer -day8 Roman to Integer: 题目描述: Roman numerals are represented by seven ...

  8. Roman to Integer/Integer to Roman

    1 Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within ...

  9. 12:Integer to Roman(数字转为罗马数字)

    问题描述 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fro ...

  10. [LeetCode]Integer to Roman

    题目描述:(链接) Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...

最新文章

  1. 链表问题16——单链表的选择排序(python版本)
  2. 追MM与Java的23种设计模式
  3. mysql5.7配置innodb_MySQL_5.7新特性innodb-buffer-pool-size配置
  4. 嵌入式Web Service gSOAP的移植与应用(二)
  5. 机器学习第九篇:详解Adaboost算法
  6. 一步步学习微软InfoPath2010和SP2010--第八章节--使用InfoPath表单Web部件
  7. Loj#114-k大异或和【线性基】
  8. C# 繁体,简体 互转
  9. ELF文件和BIN文件
  10. 复制网页上无法选中的文字
  11. c gui和java gui_C/C++编程GUI库比较
  12. oracle sql语句_7个维度查看oracle执行计划的sql语句执行效率
  13. gdb 打印字符串全部内容
  14. PS3中文游戏合集下载
  15. MAC上Maven下载及安装
  16. Power BI 简介
  17. dd 删除引导扇区_硬盘U盘数据怎么用bootice彻底删除及清零引导记录教程
  18. html打印预览空白,win7系统下使用IE浏览器预览打印页面时显示页面空白
  19. 输入数独题目,程序输出数独的唯一解。保证所有已知数据的格式都是合法的,并且题目有唯一的解。
  20. Unity制作Roll-a-Ball游戏

热门文章

  1. mysqldump mysql.sock_mysqldump原理及实战
  2. Windows核心编程_将窗口嵌入到桌面图标下面不被遮挡 spy 分析过程
  3. express中间件和路由教程
  4. 8个让DevOps转型取得成功的关键步骤
  5. centos升级默认node版本
  6. [4/10]指定结进程名称的命令taskkill.exe
  7. 用进化的观点学习网络协议
  8. memcache的简单应用
  9. 线程管理(一)线程的创建和运行
  10. 虚拟机实时迁移解决方案