【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 ...
随机推荐
- 百度地图SnapshotReadyCallback截屏
今天碰到了地图截图的功能,不太会,查查资料知道怎么弄了,跟大家分享一下 直接上代码,弄了一个方法,将截取的图片上传至服务器,返回给我们图片路径 //获取地图截图 private void getscr ...
- apple-touch-icon-precomposed 和 apple-touch-icon属性区别
苹果safari浏览器当中apple-touch-icon-precomposed 和 apple-touch-icon属性是有区别的,之前在网上查了下相关的资料和苹果的开发文档手册,对这两中属性区别 ...
- Oracle11g完全卸载方法
一.在oracle11G以前卸载oracle会存在卸载不干净,导致再次安装失败的情况,在运行services.msc打开服务,停止Oracle的所有服务. 二. oracle11G自带一个卸载批处理\ ...
- MySQL 数据库事物隔离级别的设置
select @@tx_isolation; //查看隔离级别 set session transaction isolation level read uncommitted; //设置读未提交级别 ...
- POJ 1694 An Old Stone Game【递归+排序】
链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- 9.接口BeanPostProcessor
package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; p ...
- iOS-事件传递和响应机制
转自:http://www.jianshu.com/p/2e074db792ba 前言: 按照时间顺序,事件的生命周期是这样的: 事件的产生和传递(事件如何从父控件传递到子控件并寻找到最合适的view ...
- C++, Java和C#的编译、链接过程解析
总是感觉java是解释性语言,转载下一篇感觉写的容易理解的文章 转自 http://www.cnblogs.com/rush/p/3155665.html 1.1.1 摘要 我们知道计算机不能直接理解 ...
- [luogu4315]月下“毛景树”
[luogu4315]月下"毛景树" luogu 联赛前复习一发树剖.不会告诉你WA了4发 #define ls x<<1,l,mid #define rs x< ...
- 使用OpenSSL工具制作X.509证书的方法及其注意事项总结
版权声明:本文为博主原创文章.转载请注明出处. https://blog.csdn.net/Ping_Fani07/article/details/21622545 怎样使用OpenSSL工具生成根证 ...