问题描述:

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. sql server查看用户权限

    System.ServiceModel.FaultException: Server error. Detail: The EXECUTE permission was denied on the o ...

  2. ProgrammingError: You must not use 8-bit bytestrings...

    问题出现: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byte ...

  3. Winform选择目录路径与选择文件路径

    https://blog.csdn.net/zaocha321/article/details/52528279 using System.Collections.Generic; using Sys ...

  4. (转)Autonomous_Vehicle_Paper_Reading_List

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

  5. 深度学习课程笔记(九)VAE 相关推导和应用

    深度学习课程笔记(九)VAE 相关推导和应用 2018-07-10 22:18:03 Reference: 1. TensorFlow code: https://jmetzen.github.io/ ...

  6. PHP 时间函数time、date和microtime的区别

    一.time.date 和 microtime函数 time----返回当前的 Unix 时间戳 date----格式化一个本地时间/日期 microtime----返回当前的 Unix 时间戳和微秒 ...

  7. 使用ajax无法跨源问题总结

    参考文章: 浏览器同源政策及其规避方法 跨域资源共享 CORS 详解 使用jQuery实现跨域提交表单数据 <form action="http://v.juhe.cn/weather ...

  8. C#连接数据库open函数失败

    错误信息:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...

  9. leecode第五十三题(最大子序和)

    class Solution { public: int maxSubArray(vector<int>& nums) { int len=nums.size(); )//特殊情况 ...

  10. python入门知识点(下)

    51.函数的文档注释及作用 """ 函数的文档注释: 函数的注释文字. 必须添加到函数定义的那一行的下面一行. 好处: 当使用Ctrl + Q查看函数的使用说明文档时,能 ...