LeetCode题解 #12 Integer to Roman
题目大意:给定数字,将其转化为罗马数字的形式
罗马数字其实只有 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的更多相关文章
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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 ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- 【一天一道LeetCode】#12 Integer to Roman
一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- 【LeetCode】12. Integer to Roman 整型数转罗马数
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- 【leetcode】12. Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- LeetCode:12. Integer to Roman(Medium)
1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
随机推荐
- U盘传送容量与格式问题
问题 今天想将7.6G的文件拷到U盘里,提示u盘内存不足,其实内存为14+G. 解答 U盘格式对于U盘的传送大小有限制 下面为U盘三种不同格式的应用及优缺点 FAT32格式:为系统默认格式,具有极佳的 ...
- 【集成学习】 lightgbm原理
# lightgbm和xgboost对比: 模型精度:lightgbm≈xgboost 收敛速度:lightgbm>xgboost #
- ranch分析学习(二)
紧接上篇,今天我们来分析监督树的工作者,打工仔执行任务的人.废话不多少我们直接进入正题. 3.ranch_server.erl 整个文件的功能主要是存储tcp对应参数的的信息.信息的存储方式采用的 ...
- Javascrpt 速成篇】 三:js事件处理
ie和chrome,firefox的事件处理,除了函数名字不同,基本大同小异.这样就已chrome为主了,对ie有兴趣的自己去百度.jquery已经处理不同浏览器兼容性问题,推荐使用. 事件处理有两种 ...
- C的文件操作函数
fgetc(FILE *)意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节fputc(char,FILE*)将字符ch写到文件指针fp所指向的文件的当前写指针的 ...
- BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)
题目链接 splay: #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3f3f3f ...
- ruby 的数组操作
转自:http://fujinbing.iteye.com/blog/1126232 1. & [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] # => [1, 3] ...
- Java 自定义FTP连接池
转自:https://blog.csdn.net/eakom/article/details/79038590 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
- BZOJ4145 [AMPPZ2014]The Prices
题意 你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i][j],求最小总费用. \(n \leq 100,m \leq 16\) 分析 ...
- Linux下gdb线程的调试
多线程的调试命令 1.info threads: 这条命令显示的是当前可调试的所有线程,GDB会给每一个线程都分配一个ID.前面有*的线程是当前正在调试的线程. 2.thread ID: 切换到当前调 ...