String M[] = {"", "M", "MM", "MMM”};//1000~3000
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM”};//100~900
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC”};//10~90
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX”};//1~9
 
按照上述对应规则可以进行选择合适的罗马数字。
 
参考代码:
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 整数转为罗马数字
*/
public class Solution12 {
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 整数转为罗马数字

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

  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 整数转化成罗马数字

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

  4. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  5. 【LeetCode】Integer to Roman(整数转罗马数字)

    这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...

  6. Leetcode 12——Integer to Roman

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

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

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

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

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

  9. [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 ...

随机推荐

  1. 分享9款最新超酷HTML5/CSS3应用插件

    新的一周开始了,小编继续要为大家分享实用超酷的HTML5应用,今天分享的这9款最新HTML5/CSS3应用你一定会很喜欢,一起来看看. 1.HTML5 Canvas模拟衣服撕扯动画 超级逼真 今天又要 ...

  2. JavaScript 编码小技巧

    三元操作符 如果使用if...else语句,那么这是一个很好节省代码的方式. Longhand: const x = 20; let answer; if (x > 10) { answer = ...

  3. 【玩转Golang】slice切片的操作——切片的追加、删除、插入等

    一.一般操作 1,声明变量,go自动初始化为nil,长度:0,地址:0,nil func main(){ var ss []string; fmt.Printf("length:%v \ta ...

  4. UIImage 读取图片内存优化

    在图片处理时,我们总会遇到一些内存优化的问题. ​这里介绍的是其中一种内存优化处理方式. 场景: App 运行很卡,然后我用 Instruments 中的相关工具查看对象的内存占用情况,发现当图片加载 ...

  5. LinuxMint下tty.js的安装指南

    1.简介 tty.js是使用Node.js开发的开源Web-based SSH.通过浏览器即可远程访问shell. 关于Web-based SSH的介绍参考https://en.wikipedia.o ...

  6. mysql覆盖索引(屌的狠,提高速度)

    话说有这么一个表: CREATE TABLE `user_group` ( `id` int(11) NOT NULL auto_increment, `uid` int(11) NOT NULL, ...

  7. java okhttp包的类特点

    1.开始使用这个包时候不习惯,觉得api用起来很别扭,不管是Request okhttpClient formBody只要是设置啥,就必须使用类里面的Builder类,然后一个方法接受一个参数,不停地 ...

  8. [原]unity3d 纹理旋转

    纹理旋转实现思路:纹理坐标*平移矩阵*旋转矩阵(类似顶点旋转): 矩阵一般要求中心点为(0,0) 而纹理中心点默认(0.5,0.5);所以先得平移到(0,0):可以考虑乘以平移矩阵[1,0,0,0,1 ...

  9. Eclipse------如何将项目通过maven编译并打包

    1.右击项目>>>点击Debug As>>>点击 Maven install进行编译,编译成功后入图 2.右击项目>>>点击Debug As> ...

  10. Java通过复选框控件数组实现添加多个复选框控件

    编写程序,通过复选框控件数组事先选择用户爱好信息的复选框,在该程序中,要求界面中的复选框数量可以根据指定复选框名称的字符串数组的长度来自动调节. 思路如下: 创建JPanel面板对象: 使用JPane ...