问题描述:

Given an integer, convert it to a roman numeral.

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

分析:

/**
* 将一个Integer数字转换为罗马数字,范围 1-3999
* integer数据与罗马数字之间的对应关系,列举出来,建立一个二维数组
* 1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

10~90: {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

100~900: {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

1000~3000: {"M", "MM", "MMM"}.
*/

算法:

/**
* int 类型数字转换为罗马数字
* @param num
* @return
*/
public static String intToRoman(int num){ //注意给0设置 "",罗马数字中不存在0的对应
String[][] iTor = {
   {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},//0,1-9
   {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},//0,10-90
   {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},//0,100-900
   {"", "M", "MM", "MMM"}//0,1000-3000
};
  String roman = "";
  int d = 0;
  while(num != 0){
    int pos = num % 10;
    roman = iTor[d][pos] + roman; //注意字符串拼接的顺序
    d++;
    num = num / 10;
  }
  return roman;
}

Integer To Roman leetcode java的更多相关文章

  1. Integer to Roman - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Integer to Roman - LeetCode 注意点 考虑输入为0的情况 解法 解法一:从大到小考虑1000,900,500,400,100,9 ...

  2. Integer to Roman -- LeetCode 012

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

  3. integer to roman leetcode c++实现

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

  4. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  5. LeetCode: Integer to Roman 解题报告

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

  6. leetCode练题——12. Integer to Roman

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

  7. 【LeetCode】Roman to Integer & Integer to Roman

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

  8. 【leetcode】Integer to Roman

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

  9. 【leetcode】Integer to Roman & Roman to Integer(easy)

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

随机推荐

  1. centos6.5下安装mysql5.6

    链接: https://blog.csdn.net/liangzhuoxun/article/details/81572094 该链接有个错误: 让修改的profile文件立即生效的命令为./etc/ ...

  2. AtCoder Beginner Contest 120 解题报告

    为啥最近都没有arc啊... A - Favorite Sound #include <algorithm> #include <iostream> #include < ...

  3. (转)Autonomous_Vehicle_Paper_Reading_List

    Autonomous_Vehicle_Paper_Reading_List 2018-07-19 10:40:08 Reference:https://github.com/ZRZheng/Auton ...

  4. C++笔记(2018/2/6)

    引用 & 某个变量的引用,等价于这个变量,相当于该变量的一个别名. 定义引用时一定要将其初始化成引用某个变量. 初始化后,它就一直引用该变量,不会再引用别的变量了. 通过引用所做的读写操作,会 ...

  5. Win10下Java开发环境配置

    首先下载符合操作系统版本的jdk,比如64位的JDK8: 下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...

  6. 【OData】使用Odata获取数据之后再次获取可能得不到最新的数据问题记录

    工作上遇到个问题是关于系统后台数据库更新了某数据后, 前台界面刷新显示的不是最新的数据.但是大约10分后再次刷新就能显示新的数据,或者重启IIS等web server host. 最开始认为可能是因为 ...

  7. Sublime Text 查找时排除指定的文件夹或文件

    Sublime Text 查找时排除指定的文件夹或文件 Ctrl + Shift + F这组快捷键可以调出 Sublime Text 的查找替换窗口,里边有一栏 Where,可以做一些高级设置:d:\ ...

  8. 前端性能优化之按需加载(React-router+webpack)

    一.什么是按需加载 和异步加载script的目的一样(异步加载script的方法),按需加载/代码切割也可以解决首屏加载的速度. 什么时候需要按需加载 如果是大文件,使用按需加载就十分合适.比如一个近 ...

  9. 改变input中的placeholder样式

    1.input[placeholder]{ color:#d5d5d5; } 2.input::-moz-placeholder { color: #d5d5d5; } input:-ms-input ...

  10. 使用JS语句,利用for循环的方法创建表格的两种方法

    首先去layui官网下载教程示例,在项目中加载layui.css,layui.js,JQuery.js 第一种: 将jsp语句写成字符串的形式,使用document.write()方式输出: 代码如下 ...