【leetcode刷题笔记】Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
题解:基本的罗马字符和数字对应如下表所示:
罗马字符 | 数字 |
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表:
罗马字符 | 数字 |
IV | 4 |
IX | 9 |
XL | 40 |
XC | 90 |
CD | 400 |
CM | 900 |
接下来我们用numbers记录上述13个数字,symbol数组记录上述13个符号,然后从最大的数1000开始,一步步将num转换成数字。
数字3012的转换过程如下:
- 3012/1000 = 3,answer = MMM,num <- 3012-3000=12;
- 12/900=0; 12/500 = 0; ......
- 12/10 = 1, answer = MMM+X=MMMX, num <- 12 - 10 = 2;
- 2/9 = 0,; 2/5 = 0; 2/4 = 0;
- 2/1 = 2, answer = MMMX+II = MMMXII, num <- 2-2 = 0;
所以3012对应的罗马数字为MMMXII。
代码如下:
public class Solution {
public String intToRoman(int num) {
if(num <= 0)
return ""; int[] numbers = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] symbol = {"M","CM","D","CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; StringBuffer answer = new StringBuffer();
int digit = 0;
while(num > 0){
int times = num / numbers[digit];
for(int i = 0;i < times;i++)
answer.append(symbol[digit]);
num = num - numbers[digit] * times;
digit++;
}
return answer.toString();
}
}
【leetcode刷题笔记】Integer to Roman的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- 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. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- 【leetcode刷题笔记】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
随机推荐
- php编译参数选项 具体参数含义可以用./configure --help来查看
php编译参数选项 PHP_INSTALL_PATH=/data/web/php MYSQL_INSTALL_PATH=/data/web/mysql ./configure --prefix=${ ...
- ProjectManager Beta 7 项目管理器发布
上次在Alpha阶段有一个可用版本Alpha 8也在这个博客发布了,传送:http://www.cnblogs.com/deali/p/ProjectManager.html ProjectManag ...
- Jmeter 05 JMeter元件详解
1. JMeter 逻辑控制器 Switch条件控制器.While条件控制器.交替控制器.仅一次控制器.随机控制器.随机顺序控制器.条件控制器(如果(if)).循环控制器.录制控制器.ForEach控 ...
- And Then There Was One(约瑟夫环)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- AFN多文件进度下载
AFN参考资料 http://www.jianshu.com/p/c36159094e24 http://blog.cnbang.net/tech/2320/http://blog.cnbang.ne ...
- iOS 符号化崩溃日志
1.获取一下三个文件 1. crash报告(.crash文件) 2. 符号文件 (.dsymb文件) 3. 应用程序文件 (appName.app文件,把IPA文件后缀改为zip,然后解压,Pay ...
- php本周、本月的第一天 / 最后一天的时间
//week $time1 = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, ...
- Mac下nginx安装和配置
nginx安装 brew search nginx brew install nginx 安装完以后,可以在终端输出的信息里看到一些配置路径: /usr/local/etc/nginx/nginx.c ...
- Linux c编程:I/O多路复用之epoll
前面介绍了select处理,这一章继续介绍另外一种I/O多路服用的机制:epoll.来比较下两种机制的不同点. select: 调用过程如下: (1)使用copy_from_user从用户空间拷贝fd ...
- shell 日期加减运算
比如今日是2012-04-22 $ date -d "+1 day" +%Y-%m-%d 2012-04-23 $ date -d "-1 day" +%Y ...