问题描述:

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. vue 弹性布局 实现长图垂直居上,短图垂直居中

    vue 弹性布局 实现长图垂直居上,短图垂直居中 大致效果如下图,只考虑垂直方向.长图可以通过滚动条看,短图居中效果,布局合理 html代码(vue作用域内): <div class=" ...

  2. Neo4j 文档

    Graph Fundamentals 基础 Basic concepts to get you going. A graph database can store any kind of data u ...

  3. 【sql server】索引详解

    索引可以理解为一种特殊的目录结构. sql server提供两种索引形式: 聚集索引和非聚集索引. 怎么理解这两种形式. 拿我们常用的字典举例来说, 一个字典好比数据库中的一个表.那么当我们想从字典中 ...

  4. Twitter开发2

    There are different API families The standard (free) Twitter APIs consist of REST APIs and Streaming ...

  5. Using git-flow to automate your git branching workflow

    Using git-flow to automate your git branching workflow Vincent Driessen’s branching model is a git b ...

  6. QT中QLineEdit的editingFinished()信号在按下回车时会触发两次的解决办法

    class MyLineEdit : public QLineEdit { Q_OBJECT public: MyLineEdit(QWidget * parent = 0) : QLineEdit( ...

  7. Latex: 解决 The gutter between columns is x inches wide (on page x), but should be at least 0.2 inches. 问题

    参考: Sample_WCCI.tex Latex: 解决 The gutter between columns is x inches wide (on page x), but should be ...

  8. NOI1999 生日蛋糕

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; #define I ...

  9. CSS垂直居中查询宝典

    一.垂直居中的用处 设计稿需求 当我们抱怨设计反复不定的时候,试着理解一下.每一位开发者也会是一位用户,请多多用'用户'的角色去开发.就比如下面这图,你会更稀饭哪种格式呢? 如果我们使用一个webap ...

  10. Linux——vi的使用

    记录一下vi的一些使用指令,蓝色部分是比较常用的,其中使用过的重新进行了描述,极少部分是未使用过的,还有一些未使用也未记录进来,后续再来补充修正: 参考资料:http://cn.linux.vbird ...