问题描述:

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. java 之 xml 之解析工具jaxp

    一.jaxp的api查看 *jaxp是javase一部分 *jaxp解析器在jdk的javax.xml.parsers包里面 *共四个类:分别针对dom和sax解析使用的类 *dom解析类: Docu ...

  2. P2057 [SHOI2007]善意的投票

    思路 简单的最小割模型 最小割的模型就是选出一些边,把点集划分成S和T两个部分,使得代价最小 到这题上就是板子了 代码 #include <cstdio> #include <alg ...

  3. 剥开比原看代码12:比原是如何通过/create-account-receiver创建地址的?

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  4. cmd中utf-8编码的问题

    有时候我们需要使用cmd显示某个utf-8编码的文本,这时候就需要设置cmd的代码页为65100. 也就是 chcp 65001 这条命令.这样设置可以临时生效. 如何要永久生效,需要在注册表中修改. ...

  5. DAG最小路径点覆盖

    Problem 给出一个有向无环图 (\(DAG\)),求出最少使用其中多少条互不相交的路径覆盖所有点. Solution 若有 \(n\) 个点,对于每个点 \(i\) ,我们将它拆成两个点 \(i ...

  6. 51nod 1672 区间交(贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 题意: 思路:其实这就是一个经典的区间贪心问题,只需要按照左端点排 ...

  7. memset()函数用法及其作用

    memset()函数原型是: extern void *memset(void *buffer, int c, int count) //buffer:为指针或是数组, //c:是赋给buffer的值 ...

  8. 【五】php 面向对象

    面向对象 概念:按类进行分类,类是表示彼此之间可能互不相同,但是具有一些共同点的对象集合 多态性:不同的类对同一操作可以有不同的行为 继承:允许我们使用子类在类之间创建层次关系 类 关键字:class ...

  9. 力扣(LeetCode) 217. 存在重复元素

    给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true ...

  10. 力扣(LeetCode) 771. 宝石与石头

    给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...