Leetcode:integer_to_roman
一、 题目
将给定的数字(阿拉伯数字)转化成罗马数字。
数字不会大于3999
二、 分析
首先我们要知道神马是罗马数字,尽管听说过。但事实上我还真没有记住,于是就google了下,具体见下:
个位数举例
I, 1 】II, 2】 III, 3】 IV, 4 】V, 5 】VI, 6】 VII, 7】 VIII,8 】IX, 9
十位数举例
X, 10】 XI, 11 】XII, 12】 XIII, 13】 XIV, 14】 XV, 15 】XVI, 16 】XVII, 17 】XVIII, 18】 XIX, 19】 XX, 20】 XXI, 21 】XXII, 22 】XXIX, 29】 XXX, 30】 XXXIV, 34】 XXXV, 35 】XXXIX, 39】 XL, 40】 L, 50 】LI, 51】 LV, 55】 LX, 60】 LXV, 65】 LXXX,
80】 XC, 90 】XCIII, 93】 XCV, 95 】XCVIII, 98】 XCIX, 99 】
百位数举例
C, 100】 CC, 200 】CCC, 300 】CD, 400】 D, 500 】DC,600 】DCC, 700】 DCCC, 800 】CM, 900】 CMXCIX,999】
千位数举例
M, 1000】 MC, 1100 】MCD, 1400 】MD, 1500 】MDC, 1600 】MDCLXVI, 1666】MDCCCLXXXVIII, 1888 】MDCCCXCIX, 1899 】MCM, 1900 】MCMLXXVI,1976】 MCMLXXXIV, 1984】 MCMXC, 1990 】MM, 2000 】MMMCMXCIX, 3999】
正好有我们要求的最大数。
于是我们能够想到,我们仅仅须要将数字与上面的数字相应就可以。
1. 首先。我们要先确定高位即千位的数字,即推断到底是1000还是2000还是3000。于是能够想到每次将num减去1000。并加上一个”M”。
2. 然后,我们推断百位的数字。即能够用num/100来得到百位的数字
3. 再次就是十位和个位,同2的做法
将每个位的字母从左到右连接在一起即构成了我们的结果。
另外,在CSDN上看到一个童鞋的方法非常好
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
当中每两个阶段的之间有一个减法的表示,比方900=CM, C写在M前面表示M-C。范围给到3999,情况不多直接打表事实上更快。用代码推断表示预计比較繁琐。
然后就是贪心的做法,每次选择能表示的最大值,把相应的字符串连起来。
class Solution {
public:
string intToRoman(int num) {
string str;
char *hu[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
char *te[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
char *on[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
while(num>=1000) {
str+="M";
num-=1000;
}
str += hu[num/100];
num=num%100;
str += te[num/10];
num=num%10;
str+=on[num];
return str;
}
};
法2:
class Solution {
public:
string intToRoman(int num) {
string str;
string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[]= {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
for(int i=0;num!=0;++i)
{
while(num>=value[i])
{
num-=value[i];
str+=symbol[i];
}
}
return str;
}
};
法3:JAVA
class Solution {
public:
string intToRoman(int num) {
int[] values = {1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
String[] numerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
StringBuilder result = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (number >= values[i]) {
number -= values[i];
result.append(numerals[i]);
}
}
return result.toString();
}
};
Leetcode:integer_to_roman的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)
D. Innokenty and a Football League time limit per test 2 seconds memory limit per test 256 megabytes ...
- Compare, sort, and delete duplicate lines in Notepad ++
Compare, sort, and delete duplicate lines in Notepad ++ Organize Lines: Since version 6.5.2 the app ...
- POJ 2975 Nim(博弈论)
[题目链接] http://poj.org/problem?id=2975 [题目大意] 问在传统的nim游戏中先手必胜策略的数量 [题解] 设sg=a1^a1^a3^a4^………^an,当sg为0时 ...
- Spring.NET的中间数据层(Middle Tier Data Access)——事务管理(Transaction management)
简介 Spring.NET为事务管理提供了一个持久化抽象(consistent abstraction ),其优点如下: 为不同事务API,例如ADO.NET,Enterprise Services, ...
- Ubuntu 14 下,命令行终端显示短路径
Ubuntu的终端命令行默认是长路径,即把路径深度全部显示出来,操作起来不是很方便,下面介绍命令行显示短路径的操作: $ vi ~/.bashrc 找到PS1= 的行,将\w(小写)改成\W(大写 ...
- Easyui的numberbox无法输入以0开头的数字编号(转载)
1.问题 项目中碰到这样一个问题,Easyui的numberbox在输入数字编号的时候不能以0开头 在我输入以0开头的数字编号后,离开输入框的时候,那个前缀0就自动去掉了. 接下来,我们查看API说明 ...
- Docker实践2:安装Docker及weblogic镜像
安装Docker 以root登录,运行 vi /etc/yum.repos.d/public-yum-ol6.repo,添加如下段落 [ol6_addons]name=Oracle Linux $re ...
- crontab 写入文件目录
一.crontab 目录 [root@next-cloud-server etc]# cd /var/spool/cron/ [root@next-cloud-server cron]# ls roo ...
- Spark Streaming从Flume Poll数据案例实战和内幕源码解密
本节课分成二部分讲解: 一.Spark Streaming on Polling from Flume实战 二.Spark Streaming on Polling from Flume源码 第一部分 ...
- javascript 递归函数调用(recursive funciton call)
所谓的递归函数调用,就是自己调用自己的函数. var timerHandler = null; function a(){ console.log(123); timerHandler = setTi ...