12. Integer to Roman
来源:互联网 发布:淘宝最低折扣调整失败 编辑:程序博客网 时间:2024/05/22 13:05
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Solution 1
11ms 25.74%
public class Solution { public String intToRoman(int num) { String M[] = {"", "M", "MM", "MMM"}; String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]; }}
Solution 2
8ms 69.86%
public class Solution { public String intToRoman(int num) { int values[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };String[] strs = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };StringBuilder sb = new StringBuilder();for (int i = 0; i < values.length; i++) {while (num >= values[i]) {num -= values[i];sb.append(strs[i]);}}return sb.toString(); }}
public enum Type {M(1000), CM(900), D(500), CD(400), C(100), XC(90), L(50), XL(40), X(10), IX(9), V(5), IV(4), I(1);private final int value;Type(int value) {this.value = value;}};public String intToRoman3(int num) {StringBuilder output = new StringBuilder();for (Type t : Type.values()) {while (num >= t.value) {output.append(t);num -= t.value;}}return output.toString();}
0 0
- 12.Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- Java多线程
- Java认证考试实例疑难辨析(3)
- java 基础总结 -- enum 枚举的基础使用
- SpatialHadoop中空间索引系列之(八)现有空间数据库的索引方式
- SpatialHadoop中空间索引系列之(九)B树索引
- 12. Integer to Roman
- 【树状数组】2016 BUPT Winter Training #2 D
- SurfaceView初步
- #Android源码#View的构造方法为什么要4个?
- 拜个年
- 开始使用linggle
- http://blog.csdn.net/MyArrow/article/category/1054647
- linux配置nginx虚拟目录
- Android TV - 1.2 Handling TV Hardware