12.Integer to Roman

  Given an integer, convert it to a roman numeral.

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

  拿到题目,分析,比较简单,除掉相应的基数单位,拼接起来就可以,不过要注意4,9这些特殊的表示。

先上我的代码吧;

public String intToRoman(int num){
StringBuffer sb=new StringBuffer(); while(num!=0){
if(num>=1000){
append(sb, "M", num/1000);
num=num%1000;
}else if(num>=500){
if(num>=900){
append(sb,"CM",1);
num=num-900;
}else{
append(sb,"D",num/500);
num=num%500;
}
}else if(num>=100){
if(num>=400){
append(sb,"CD",1);
num=num-400;
}else{
append(sb,"C",num/100);
num=num%100;
}
}else if(num>=50){
if(num>=90){
append(sb, "XC", 1);
num=num-90;
}else{
append(sb,"L",num/50);
num=num%50;
}
}else if(num>=10){
if(num>=40){
append(sb,"XL",1);
num=num-40;
}else{
append(sb,"X",num/10);
num=num%10;
}
}else if(num>=5){
if(num>=9){
append(sb,"IX",1);
num=num-9;
}else{
append(sb,"V",num/5);
num=num%5;
}
}else{
if(num==4){
append(sb,"IV",1);
num=num-4;
}else{
append(sb,"I",num);
num=0;
}
}
}
return sb.toString();
}
public static void append(StringBuffer sb,String str,int times){
for(int i=0;i<times;i++){
sb.append(str);
}
}

但是呢,当我A掉之后,再去看这上面的第一个答案,跪了,又快又简单。用数组表示要放的数字。

public static 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];
}

Leetcode 12——Integer to Roman的更多相关文章

  1. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  2. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  3. [LeetCode] 12. Integer to Roman ☆☆

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  4. [LeetCode] 12. Integer to Roman 整数转为罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  5. Java [leetcode 12] Integer to Roman

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

  6. [leetcode]12. Integer to Roman整数转罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  7. LeetCode——12. Integer to Roman

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

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

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

  9. [leetcode] 12. Integer to Roman

    关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...

随机推荐

  1. 图片压缩上传Thumbnailator 插件

    一,接口已经写死 public static String upload(String appCode, MultipartFile inputFile) public static String u ...

  2. 4-20mA 意义

    工业上最广泛采用的标准模拟量电信号是用4~20mA直流电流来传输模拟量. 采用电流信号的原因是不容易受干扰.并且电流源内阻无穷大,导线电阻串联在回路中不影响精度,在普通双绞线上可以传输数百米.上限取2 ...

  3. springMVC web项目 对访问数据库的用户名密码进行加密解密

    在使用springMVC开发web项目中,数据库的用户名,密码一般都是配置在.properties文件中 然后在通过.xml配置文件引入.properties的变量,例如 在config.proper ...

  4. 纯css实现图片的灯光照射效果,高逼格图片展示

    先不说技术,看实现的效果, 与原图(左图)相比,‘灯光’ 照射(右图)下的小姐姐是不是更有魅力了!! 那么下面就说说大家关心的技术实现过程. 其实这是我在学习css属性 mix-blend-mode ...

  5. linux升级python3.6相关命令

    sudo apt-get install python3.6 sudo update-alternatives --install /usr/bin/python python /usr/bin/py ...

  6. Spark ML源码分析之一 设计框架解读

    本博客为作者原创,如需转载请注明参考           在深入理解Spark ML中的各类算法之前,先理一下整个库的设计框架,是非常有必要的,优秀的框架是对复杂问题的抽象和解剖,对这种抽象的学习本身 ...

  7. 小程序wx.navigateTo和wx.redirectTo 都无效

    最近在写小程序,遇到页面跳转时,发现有几次失败.查询资料已解决,总结一下知识点: 一.如下,第5层到到6层时失败(评论页⑤-->返回商品详情页⑥) 登陆①-->主页②-->商品列表页 ...

  8. createjs绘制扇形的方法

    扇形由三段线条组成,两条直线和一条弧线,直线可以用createjs中的lineTo函数画出,弧线用Graphics.arc函数来画. 一.关于createjs中的Graphics.Arc API Gr ...

  9. kill 掉所有正在运行的hadoop jobs

    # get list of job's process IDs JOB_LIST=$(hadoop job -list 2> /dev/null | grep job_ | awk '{prin ...

  10. 不能为虚拟电脑 ubuntu 打开一个新任务.

    使用virtualbox报错: 不能为虚拟电脑 ubuntu3 打开一个新任务. The virtual machine 'ubuntu3' has terminated unexpectedly d ...