问题描述:

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. S.M.A.R.T.记录几块ssd硬盘

    1.闪迪至尊超级速(Extreme pro) 2.三星sm961 (m2接口) 3.intel 750 (pice接口) ps: 因为sm961,intel750都是nvme协议,网上大部分软件测试都 ...

  2. NodeJs完全迁移到D盘,形成绿色安装版

    查看npm配置 D:\Git\AG-Admin-v2. (master) $ npm config ls ; cli configs metrics-registry = "https:// ...

  3. P1829 [国家集训队]Crash的数字表格 / JZPTAB

    推式子太快乐啦!虽然我好蠢而且dummy和maomao好巨(划掉) 思路 莫比乌斯反演的题目 首先这题有\(O(\sqrt n)\)的做法但是我没写咕咕咕 然后就是爆推一波式子 \[ \sum_{i= ...

  4. HDU 5607 graph(矩阵乘法)

    题意 在一个 \(n\) 个节点 \(m\) 条边的有向图上随机游走,有 \(Q\) 个询问,每次给定一个起点 \(u\) 和步数 \(K\) ,每次回答最后停在每个节点的概率. \(1 \leq n ...

  5. 使用Java Api 操作HDFS

    如题 我就是一个标题党  就是使用JavaApi操作HDFS,使用的是MAVEN,操作的环境是Linux 首先要配置好Maven环境,我使用的是已经有的仓库,如果你下载的jar包 速度慢,可以改变Ma ...

  6. Jupyter Notebook主题字体设置及自动代码补全

    Jupyter Notebook用久了就离不开了,然而自带的主题真的不忍直视.为了视力着想,为了自己看起来舒服,于是折腾了一番..在github上发现了一个jupyter-themes工具,可以通过p ...

  7. jquery选择器扩展之样式选择器

    https://github.com/wendux/style-selector-jQuery-plugin http://blog.csdn.net/duwen90/article/details/ ...

  8. js作用域题目

    window.number = 4var obj = { 'number': 4, 'tbl': (function(){ this.number *= 2; console.log(this.num ...

  9. python实现八皇后问题

    import random def judge(state, nextX): #判断是否和之前的皇后状态有冲突 nextY = len(state) for i in range(nextY): if ...

  10. C#——LINQ语句

    委托: //delegate 返回值 委托名(参数); //委托不能在方法中定义 ////实例化委托,并赋值 //委托名 实例名 = new 委托名(函数名).lambda表达式; //使用委托实例, ...