12. Integer to Roman
2015-04-16 09:34
176 查看
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
链接: http://leetcode.com/problems/integer-to-roman/
题解:
数值计算的题目,需要了解罗马数字的规律。 Time Complexity - O(n), Space Complexity - O(n)
二刷:
I, IV, V, IX, X, XL, L, XC, C, CD, D, CM, M分别代表 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000
Java:
Python:
三刷:
Java:
Reference:
https://leetcode.com/discuss/49870/my-java-solution-easy-to-understand
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
链接: http://leetcode.com/problems/integer-to-roman/
题解:
数值计算的题目,需要了解罗马数字的规律。 Time Complexity - O(n), Space Complexity - O(n)
public class Solution { public String intToRoman(int num) { //Roman: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 StringBuilder result = new StringBuilder(); 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]; result.append(symbol[i]); } } return result.toString(); } }
二刷:
I, IV, V, IX, X, XL, L, XC, C, CD, D, CM, M分别代表 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000
Java:
public class Solution { private final String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; private final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; public String intToRoman(int num) { if (num <= 0) { return ""; } StringBuilder sb = new StringBuilder(); for (int i = 0; num > 0; i++) { while (num >= values[i]) { num -= values[i]; sb.append(romans[i]); } } return sb.toString(); } }
Python:
class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] res = '' for i in range(0, len(romans)): while num >= numbers[i]: num -= numbers[i] res += romans[i] return res
三刷:
Java:
public class Solution { public String intToRoman(int num) { if (num <= 0) return ""; String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; StringBuilder sb = new StringBuilder(); for (int i = 0; num > 0; i++) { while (num >= values[i]) { num -= values[i]; sb.append(romans[i]); } } return sb.toString(); } }
Reference:
https://leetcode.com/discuss/49870/my-java-solution-easy-to-understand
相关文章推荐
- leetCode 12.Integer to Roman (整数转罗马数字) 解题思路和方法
- 【12】Integer to Roman
- [LeetCode]12. Integer to Roman(整数转化为罗马数字)
- Leetcode 12. Integer to Roman(python)
- 12:Integer to Roman(数字转为罗马数字)
- leetcode 12 ,13 Integer to Roman &&Roman to Integer 罗马与阿拉伯数组转换
- leetcode[12]Integer to Roman
- LeetCode 12:"Integer to Roman"
- 12、Integer to Roman
- LeetCode_OJ【12】【13】Integer to Roman && Roman to Integer
- 12. Integer to Roman
- 12. Integer to Roman
- LeetCode 12. Integer to Roman(阿拉伯转罗马数字)
- [leetcode]12. Integer to Roman
- 12、Integer to Roman
- [leetcode 12] Integer to Roman
- Leetcode 12. Integer to Roman The Solution of Python
- 12 Integer to Roman
- leetcode-12 Integer to Roman
- leetcode-12 Integer to Roman