Given an integer, convert it to a roman numeral.

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

代码如下:

 public class Solution {
public String intToRoman(int num) {
int n=0;
int[] nums={1000,500,100,50,10,5,1};
Map<Integer,String> map=new HashMap<>();
map.put(1,"I");
map.put(10,"X");
map.put(100,"C");
map.put(1000,"M");
map.put(5,"V");
map.put(50,"L");
map.put(500,"D");
String s="";
if(map.containsKey(num))
return map.get(num); for(int i=0;i<nums.length-1;i++)
{
if(num>=nums[i])
{
n=num/nums[i]; if(n>=1&&n<=4){
if(n==4)
{
if(s.length()!=0&&s.substring(s.length()-1,s.length()).equals(nums[i-1]))
{
s=s.substring(0,s.length()-1);
s=s+map.get(nums[i]); if(nums[i]==10||nums[i]==100)
s=s+map.get(nums[i-2]);
}else{
s=s+map.get(nums[i]);
if(nums[i]==10||nums[i]==100)
s=s+map.get(nums[i-1]);
}
}
else{
while(n>0)
{s=s+map.get(nums[i]);n--;}
}
num=num%nums[i];
}
}
}
n=num/nums[nums.length-1];
if(n>=1&&n<=4){
if(n==4)
{s=s+map.get(nums[nums.length-1])+map.get(nums[nums.length-2]);}
else{
while(n>0)
{s=s+map.get(nums[nums.length-1]);n--;}
}
}
s=s.replace("VIV","IX");
s=s.replace("LXL","XC");
s=s.replace("DCD","CM");
return s;
}
}

12. Integer to Roman的更多相关文章

  1. Leetcode 12——Integer to Roman

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

  2. Leetcode 12. Integer to Roman(打表,水)

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

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

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

  4. 《LeetBook》leetcode题解(12):Integer to Roman[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. 【LeetCode】12. Integer to Roman (2 solutions)

    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 ☆☆

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

  7. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  8. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  9. 【leetcode】12. Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

随机推荐

  1. linux 执行php文件

    /opt/php5/bin/php /home/Xcar/tag/interface/tag_api4hbase.php tag_export2file "/tmp/guo.php" ...

  2. jquery删除原事件

    $(document).ready(function(){ var fn = $("#exportCsv").attr( "onclick" ); // 获取原 ...

  3. POJ 1573 Robot Motion 模拟 难度:0

    #define ONLINE_JUDGE #include<cstdio> #include <cstring> #include <algorithm> usin ...

  4. 一模 (6) day2

    第一题: 题目大意:求最长公共上升子序列(LICS): 解题过程: 1.一开始想到模仿求最长公共子序列的方法,F[i][j]表示A串前i个,B串前j个的最长公共子序列,很明显当A[i]!= B[j]时 ...

  5. python处理url中的中文编码,以及其他编码问题

    1.python中的urlencode与urldecode 2.各种编码转换在线工具 3.python用于url解码和中文解析的小脚本(python url decoder) 4.如何只对url中的中 ...

  6. C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast

    Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...

  7. [C/C++]C/C++相关网站

    1.http://en.cppreference.com What is the purpose of this site? Our goal is to provide programmers wi ...

  8. T-sql语句

    在用代码编辑数据库时,首先要启动WAMPSERVER 1.创建表 create table Car ( Code varchar(50) primary key , Name varchar(50) ...

  9. iOS 三种录制视频方式

    随着每一代 iPhone 处理能力和相机硬件配置的提高,使用它来捕获视频也变得更加有意思.它们小巧,轻便,低调,而且与专业摄像机之间的差距已经变得非常小,小到在某些情况下,iPhone 可以真正替代它 ...

  10. kali linux karmetasploit配置

    原理分析:http://www.freebuf.com/articles/77055.html 转官方说明:https://www.offensive-security.com/metasploit- ...