LeetCode 题解(58): Integer to Roman
来源:互联网 发布:java中super的作用 编辑:程序博客网 时间:2024/05/21 18:43
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
题解:罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的规则可以表示任意正整数。需要注意的是罗马数字中没有“0”,与进位制无关。一般认为罗马数字只用来记数,而不作演算。
- 重复数次:一个罗马数字重复几次,就表示这个数的几倍。
- 右加左减:
- 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
- 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。
- 左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
- 但是,左减时不可跨越一个位数。比如,99不可以用IC(
100−1 )表示,而是用XCIX([100−10]+[10−1] )表示。(等同于阿拉伯数字每位数字分别表示。) - 左减数字必须为一位,比如8写成VIII,而非IIX。
- 右加数字不可连续超过三位,比如14写成XIV,而非XIIII。(见下方“数码限制”一项。)
class Solution {public: string intToRoman(int num) { string result = ""; for(int i = 0; i < num / 1000; i++) result += "M"; num = num % 1000; string low, mid, high; if(num / 100) { low = "C"; mid = "D"; high = "M"; if(num / 100 == 4) { result += low; result += mid; } else if(num / 100 == 9) { result += low; result += high; } else if(num / 100 < 4) for(int k = 0; k < num / 100; k ++) result += low; else { result += mid; for(int l = 5; l < num / 100; l++) result += low; } num %= 100; } if(num / 10) { low = "X"; mid = "L"; high = "C"; if(num / 10 == 4) { result += low; result += mid; } else if(num / 10 == 9) { result += low; result += high; } else if(num / 10 < 4) for(int k = 0; k < num / 10; k ++) result += low; else { result += mid; for(int l = 5; l < num / 10; l++) result += low; } num %= 10; } low = "I";mid = "V"; high = "X"; if(num == 4) { result += low; result += mid; } else if(num == 9) { result += low; result += high; } else if(num < 4) for(int k = 0; k < num; k ++) result += low; else { result += mid; for(int l = 5; l < num; l++) result += low; } return result; }};
Java版:
public class Solution { public String intToRoman(int num) { String result = new String(""); for(int i = 0; i < num / 1000; i++) result = result.concat("M"); num %= 1000; String low, mid, high = new String(); if(num / 100 != 0) { low = "C"; mid="D"; high="M"; if(num / 100 == 4) result = result.concat(low).concat(mid); else if(num / 100 == 9) result = result.concat(low).concat(high); else if(num / 100 < 4) for(int j = 0; j < num / 100; j++) result = result.concat(low); else { result = result.concat(mid); for(int k = 5; k < num / 100; k++) result = result.concat(low); } num %= 100; } if(num / 10 != 0) { low = "X"; mid="L"; high="C"; if(num / 10 == 4) result = result.concat(low).concat(mid); else if(num / 10 == 9) result = result.concat(low).concat(high); else if(num / 10 < 4) for(int j = 0; j < num / 10; j++) result = result.concat(low); else { result = result.concat(mid); for(int k = 5; k < num / 10; k++) result = result.concat(low); } num %= 10; } low = "I"; mid="V"; high="X"; if(num == 4) result = result.concat(low).concat(mid); else if(num == 9) result = result.concat(low).concat(high); else if(num < 4) for(int j = 0; j < num; j++) result = result.concat(low); else { result = result.concat(mid); for(int k = 5; k < num; k++) result = result.concat(low); } return result; }}
Python版:
class Solution: # @return a string def intToRoman(self, num): result = "" for i in range(0, num / 1000): result += "M" num %= 1000 if num / 100 != 0: if num / 100 == 4: result += "C" result += "D" elif num / 100 == 9: result += "C" result += "M" elif num / 100 < 4: for i in range(num / 100): result += "C" else: result += "D" for i in range(5, num / 100): result += "C" num %= 100 if num / 10 != 0: if num / 10 == 4: result += "X" result += "L" elif num / 10 == 9: result += "X" result += "C" elif num / 10 < 4: for i in range(num / 10): result += "X" else: result += "L" for i in range(5, num / 10): result += "X" num %= 10 if num == 4: result += "I" result += "V" elif num == 9: result += "I" result += "X" elif num < 4: for i in range(num): result += "I" else: result += "V" for i in range(5, num): result += "I" return result
0 0
- LeetCode 题解(58): Integer to Roman
- LeetCode 题解(73): Roman to Integer
- LeetCode题解:Roman to Integer
- LeetCode题解:Integer to Roman
- LeetCode题解:Roman to Integer
- LeetCode题解:Integer to Roman
- LeetCode 13. Roman to Integer 题解(C++)
- LeetCode 12. Integer to Roman 题解(C++)
- leetcode题解||Integer to Roman问题
- leetcode题解 || Roman to Integer问题
- LeetCode题解——Roman to Integer
- LeetCode题解——Integer to Roman
- LeetCode 12 Integer to Roman题解
- LeetCode题解-12-Integer to Roman
- LeetCode题解-13-Roman to Integer
- 12. Integer to Roman LeetCode题解
- 13. Roman to Integer LeetCode题解
- leetcode题解-12. Integer to Roman
- 第9周项目6穷举法解决组合问题(3换分币)
- SDL2.0_03_SDL_image Library
- C++ 使用Jsoncpp解析Json
- Python核心编程 练习 第七章
- 什么是ADSL?它有哪些特点
- LeetCode 题解(58): Integer to Roman
- Java 导出EXCEL
- 第九周项目二——分数的累加
- 第九周项目 6 穷举法解决组合问题(换分币)
- PopupWindow杂谈
- 小企业智能路由器的应用 (用智能路由器推广你的烤肉饭)
- 学生A希望访问网站www.ss.edu.cn,用户A在其浏览器中输入http://www.ss.edu.cn并按回车,直到西亚斯的网站首页显示在其浏览器中,请问:在此过程中,按照TCP/IP参考模型,
- 写一个 docker 实战一个系列手册
- 简述网络设备集线器、网桥、交换机、路由器的基本原理