12. Integer to Roman
来源:互联网 发布:无线云台网络摄像机 编辑:程序博客网 时间:2024/06/11 05:45
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this question.
Solution:
Tips:
solve this problem with recursion, just for practice.
Java Code:
public class Solution { public String intToRoman(int num) { if (num <= 0) { return ""; } if (num >= 900) { return num >= 1000 ? "M" + intToRoman(num - 1000) : "CM" + intToRoman(num - 900); } else if (num >= 400) { return num >= 500 ? "D" + intToRoman(num - 500) : "CD" + intToRoman(num - 400); } else if (num >= 90) { return num >= 100 ? "C" + intToRoman(num - 100) : "XC" + intToRoman(num - 90); } else if (num >= 40) { return num >= 50 ? "L" + intToRoman(num - 50) : "XL" + intToRoman(num - 40); } else if (num >= 9) { return num >= 10 ? "X" + intToRoman(num - 10) : "IX" + intToRoman(num - 9); } else if (num >= 4) { return num >= 5 ? "V" + intToRoman(num - 5) : "IV" + intToRoman(num - 4); } else { return "I" + intToRoman(num - 1); } }}
0 0
- 12.Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- 12. Integer to Roman
- proguard
- 图的综合问题
- 运动目标检测视频Dataset
- 更改CentOS7 默认网卡eno16777736为eth0
- Vim复制代码段
- 12. Integer to Roman
- 随笔——9999成就达成
- C++类的内存对齐-C++alignment
- android developer tiny share-20170407
- 基于Linux下的C语言入门-day1
- android学习案例2——ToggleButton
- 编程题(1):笔试题-京东软件测试2018实习招聘
- poj 1064 Cable master
- ros kinetic 下编译laser_scan_matcher 方法