《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
012. Integer to Roman[M]
问题
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路
分析罗马数字的规律:
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1,000 |
上面是罗马数字所有的符号。
罗马数字的规则:
一般情况下,从左到右从大到小排,字母代表的数字累加。
比如:
XII = 12
MDCCLXVI= 1000+500+100+100+50+10+5+1
但是有特殊情况,就是,如果数字的范围在大数减小数的范围内,则会出现小数在大数前面的情况,代表(大数-小数)
IV = 5-1
IX= 10 - 1 = 9
XL = 50-10 = 40
| Symbol | Value |
|---|---|
| IV | 4 |
| IX | 9 |
| XL | 40 |
| XC | 90 |
| CD | 400 |
| CM | 900 |
思路1——循环
一旦把所有可能的情况符号情况都列举出来了,就好做了。
我们现在拿到一个数N
- 我们就去表里面找不超过它的最大的数x,
- 然后把它入我们的输出字符串中,然后将数N-=x,
- 继续执行这个操作,直到N=0
public class Solution {
public String intToRoman(int num) {
int list[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String chars[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int i = 0;
String out="";
while(num > 0)
{
for(;i < list.length;i++)
if(num >= list[i])
break;
out+=chars[i];
num -= list[i];
}
return out;
}
}
思路2——查表
还有个更极端的方案,就是,把每位上可能出现的情况都列举出来,剩下的,只用查表就行了。
public class Solution {
public static String intToRoman(int num) {
String M[] = {"", "M", "MM", "MMM"};
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}
}
《LeetBook》leetcode题解(12):Integer to Roman[M]的更多相关文章
- LeetCode题解 #12 Integer to Roman
题目大意:给定数字,将其转化为罗马数字的形式 罗马数字其实只有 I V X L C D M 这几种形式,其余均为组合的,去百度了解一下就ok. 所以首先想到的就是,将个.十.百.千位的数字构造出来,然 ...
- 【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 ...
随机推荐
- handsontable-mobiles
适配移动端:文档不完整,现在只能适配ipad4
- Lucene 分页搜索实现
Lucene中有两种分页查询方式 1.一次查询出大量数据,然后根据页码定位是哪个文档,其实就是暴力获取了 2.通过调用searchAfter来实现 我们都知道collect是lucene中对搜索到的文 ...
- cocos2dx常见Action(转)
本文转载自:http://blog.csdn.net/ff313976/article/details/23667209 bool HelloWorld::init(){ ////////////// ...
- 【MVC】输出HTML内容,不输出HTML标签
第一种方式: @Html.Raw("内容") 第二种方式 @(new HtmlString("<h1>abcd</h1>")) 第三种方 ...
- EF6 使用SQLite Code First
SQLite是一款轻型关系型数据库,做一个小网站,用来替代sql server或者access数据库应该会是一个不错的选择. ASP.NET Entity Framework 6是微软平台的著名ORM ...
- UWP开发入门(十)——通过继承来扩展ListView
本篇之所以起这样一个名字,是因为重点并非如何自定义控件,不涉及创建CustomControl和UserControl使用的Template和XAML概念.而是通过继承的方法来扩展一个现有的类,在继承的 ...
- Linux程序设计:目录维护
一.相关系统调用 1.1 chmod 改变访问权限. #include <sys/stat.h> int chmod(const char *path, mode_t mode) 1.2 ...
- IDEA不能实时更新jsp页面的问题
第一步: 第二步 第三步: 将这三个选项 改成
- long int double float
参考:https://blog.csdn.net/ideality_hunter/article/details/78432486 long是长整型,64位 int是短整型,32位 double是双精 ...
- 洛谷P4586 [FJOI2015]最小覆盖双圆问题(最小圆覆盖)
题面 传送门 前置芝士 最小圆覆盖 题解 我们按照\(x\)坐标排序,然后二分中间点,把点分成左右两边,对两边都做一个最小圆覆盖,那么半径大一点的那个就是答案了.然后对半径大的那一边继续二分就行了 然 ...