题目大意:给定数字,将其转化为罗马数字的形式

罗马数字其实只有 I V X L C D M 这几种形式,其余均为组合的,去百度了解一下就ok。

所以首先想到的就是,将个、十、百、千位的数字构造出来,然后直接用就好了。

要特别注意为整10,整100、1000的情况。

String [] ge ={"I","II","III","IV","V","VI","VII","VIII","IX"};

String [] shi = {"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};

String [] bai = {"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};

String [] qian = {"M","MM","MMM"};

public String intToRoman(int num) {

String result = "";
String g = "";
String s = "";
String b = "";
String q = "";

if(num%10>0){
g = ge[num%10-1];
num/=10;}else{

g="";
num/=10;
}

if(num>0){

if(num%10>0){
s = shi[num%10-1];
num/=10;}else{

s="";
num/=10;
}

if(num>0){

if(num%10>0){
b = bai[num%10-1];
num/=10;}else{

b="";
num/=10;
}

if(num>0){

q=qian[num%10-1];
num/=10;

return q+b+s+g;

}
else{

return b+s+g;

}

}
else{

return s+g;

}

}
else
return g;

}

更精简的代码(将整10、100、1000的情况整合到数组里面)

string intToRoman(int num) {

string roman[4][10] = {

{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},

{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},

{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},

{"", "M", "MM", "MMM"}

};

string ret = "";

int digit = 0;

while (num)

{ret = roman[digit++][num % 10] + ret, num /= 10; return ret; }

LeetCode题解 #12 Integer to Roman的更多相关文章

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

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

  2. 【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 ...

  3. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  4. 【一天一道LeetCode】#12 Integer to Roman

    一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...

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

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

  6. 【leetcode】12. Integer to Roman

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

  7. LeetCode:12. Integer to Roman(Medium)

    1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...

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

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

  9. Leetcode 12——Integer to Roman

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

随机推荐

  1. crm 02--->讲师页面及逻辑

    要求: 讲师 批量初始化 考勤 录入成绩 批量初始化 考勤与批量初始化这两个功能都要放在课程记录表中CourseRecord # 批量初始化 # 将该班的所有学生,初始化带某一天,而不是将每个学生一个 ...

  2. Intro to DBSCAN

    DBSCAN Density-Based Spatial Clustering of Application with Noise It can discover cluster of arbitra ...

  3. 使用SDWebImage加载大量图片后造成内存泄露的解决办法

    SDWebImage的知名度就不用说了,github上近10k的star,国内外太多的App使用其进行图片加载. 但是最近在使用过程中发现,在UITableView中不断加载更多的内容,使用SDWeb ...

  4. ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望||概率DP)

    Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...

  5. BZOJ1208 HNOI2004 宠物收养所 【非旋转Treap】

    BZOJ1208 HNOI2004 宠物收养所 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的 ...

  6. Create A .NET Core Development Environment Using Visual Studio Code

    https://www.c-sharpcorner.com/article/create-a-net-core-development-environment-using-visual-studio- ...

  7. LeetCode Friend Circles

    原题链接在这里:https://leetcode.com/problems/friend-circles/description/ 题目: There are N students in a clas ...

  8. {Reship}{Sparse Representation}稀疏表示入门

    声明:本人属于绝对的新手,刚刚接触“稀疏表示”这个领域.之所以写下以下的若干个连载,是鼓励自己不要急功近利,而要步步为赢!所以下文肯定有所纰漏,敬请指出,我们共同进步! 踏入“稀疏表达”(Sparse ...

  9. 安装 Ruby, Rails 运行环境

    步骤1 - 安装 RVM RVM 是干什么的这里就不解释了,后面你将会慢慢搞明白. $ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1 ...

  10. webpack 使用 extract-text-webpack-plugin 报错 Tapable.plugin is deprecated. Use new API on .hooks instead

    webpack 使用 extract-text-webpack-plugin 报错 Tapable.plugin is deprecated. Use new API on .hooks instea ...