leedCode练题——12. Integer to Roman

1、题目

12. Integer to Roman

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one‘s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: 3
Output: "III"

Example 2:

Input: 4
Output: "IV"

Example 3:

Input: 9
Output: "IX"

Example 4:

Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

2、我的解答(未简化)

# -*- coding: utf-8 -*-# @Time    : 2020/1/30 19:14# @Author  : SmartCat0929# @Email   : [email protected]# @Link    : https://github.com/SmartCat0929# @Site    : # @File    : 12. Integer to Roman(simplify).py

class Solution:    def intToRoman(self, num: int) -> str:        numStr = str(num)        if num < 10:            if int(numStr[-1]) > 0 and int(numStr[-1]) < 4:                units = ""                for i in range(0, int(numStr[-1])):                    units = "I" + units            elif int(numStr[-1]) == 4:                units = "IV"            elif int(numStr[-1]) == 5:                units = "V"            elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:                units = "V"                for i in range(5, int(numStr[-1])):                    units = units + "I"            elif int(numStr[-1]) == 9:                units = "IX"            return units        elif num >= 10 and num <= 99:  # 如果是两位数            if int(numStr[-1]) == 0:                units = ""            elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4:  # 个位                units = ""                for i in range(0, int(numStr[-1])):                    units = "I" + units            elif int(numStr[-1]) == 4:                units = "IV"            elif int(numStr[-1]) == 5:                units = "V"            elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:                units = "V"                for i in range(5, int(numStr[-1])):                    units = units + "I"            elif int(numStr[-1]) == 9:                units = "IX"            if int(numStr[-2]) == 1:  # 十位                decades = "X"            elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4:  # 十位                decades = ""                for j in range(0, int(numStr[-2])):                    decades = "X" + decades            elif int(numStr[-2]) == 4:                decades = "XL"            elif int(numStr[-2]) == 5:                decades = "L"            elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9:                decades = "L"                for j in range(5, int(numStr[-2])):                    decades = decades + "X"            elif int(numStr[-2]) == 9:                decades = "XC"            romans = decades + units            return romans

        elif num >= 100 and num <= 999:  # 如果是三位数            if int(numStr[-1]) == 0:  # 个位                units = ""            elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4:                units = ""                for i in range(0, int(numStr[-1])):                    units = "I" + units            elif int(numStr[-1]) == 4:                units = "IV"            elif int(numStr[-1]) == 5:                units = "V"            elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:                units = "V"                for i in range(5, int(numStr[-1])):                    units = units + "I"            elif int(numStr[-1]) == 9:                units = "IX"            if int(numStr[-2]) == 0:  # 十位                decades = ""            elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4:                decades = ""                for j in range(0, int(numStr[-2])):                    decades = "X" + decades            elif int(numStr[-2]) == 4:                decades = "XL"            elif int(numStr[-2]) == 5:                decades = "L"            elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9:                decades = "L"                for j in range(5, int(numStr[-2])):                    decades = decades + "X"            elif int(numStr[-2]) == 9:                decades = "XC"            if int(numStr[-3]) > 0 and int(numStr[-3]) < 4:  # 百位                hundreds = ""                for j in range(0, int(numStr[-3])):                    hundreds = "C" + hundreds            elif int(numStr[-3]) == 4:                hundreds = "CD"            elif int(numStr[-3]) == 5:                hundreds = "D"            elif int(numStr[-3]) > 5 and int(numStr[-3]) < 9:                hundreds = "D"                for j in range(5, int(numStr[-3])):                    hundreds = hundreds + "C"            elif int(numStr[-3]) == 9:                hundreds = "CM"            romans = hundreds + decades + units            return romans

        elif num >= 1000 and num <= 3999:  # 如果是四位数            if int(numStr[-1]) == 0:  # 个位                units = ""            elif int(numStr[-1]) > 0 and int(numStr[-1]) < 4:                units = ""                for i in range(0, int(numStr[-1])):                    units = "I" + units            elif int(numStr[-1]) == 4:                units = "IV"            elif int(numStr[-1]) == 5:                units = "V"            elif int(numStr[-1]) > 5 and int(numStr[-1]) < 9:                units = "V"                for i in range(5, int(numStr[-1])):                    units = units + "I"            elif int(numStr[-1]) == 9:                units = "IX"            if int(numStr[-2]) == 0:  # 十位                decades = ""            elif int(numStr[-2]) > 0 and int(numStr[-2]) < 4:                decades = ""                for j in range(0, int(numStr[-2])):                    decades = "X" + decades            elif int(numStr[-2]) == 4:                decades = "XL"            elif int(numStr[-2]) == 5:                decades = "L"            elif int(numStr[-2]) > 5 and int(numStr[-2]) < 9:                decades = "L"                for j in range(5, int(numStr[-2])):                    decades = decades + "X"            elif int(numStr[-2]) == 9:                decades = "XC"            if int(numStr[-3]) == 0:  # 百位                hundreds = ""            elif int(numStr[-3]) > 0 and int(numStr[-3]) < 4:                hundreds = ""                for j in range(0, int(numStr[-3])):                    hundreds = "C" + hundreds            elif int(numStr[-3]) == 4:                hundreds = "CD"            elif int(numStr[-3]) == 5:                hundreds = "D"            elif int(numStr[-3]) > 5 and int(numStr[-3]) < 9:                hundreds = "D"                for j in range(5, int(numStr[-3])):                    hundreds = hundreds + "C"            elif int(numStr[-3]) == 9:                hundreds = "CM"            if int(numStr[-4]) > 0 and int(numStr[-4]) < 4: #千位                thousands = ""                for j in range(0, int(numStr[-4])):                    thousands = "M" + thousands                romans = thousands + hundreds + decades + units                return romans        elif num > 3999:            return 0

print(Solution().intToRoman(400))

原文地址:https://www.cnblogs.com/Smart-Cat/p/12244011.html

时间: 2024-10-14 13:53:25

leedCode练题——12. Integer to Roman的相关文章

leetCode 12. Integer to Roman | 字符串 | Medium

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题目大意: 将一个给定的阿拉伯数字转换成罗马数字. 思路: 这题看到的时候,想的太多. 其实很简单,将千位,百位,十位,个位都表示出来,然后组合即可. 代码如下: class Solution { public:     string int

12. Integer to Roman &amp;&amp; 13. Roman to Integer &amp;&amp; 273. Integer to English Words

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

[LeetCode] 12. Integer to Roman ☆☆

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 解释: 罗马数字采用七个罗马字母作数字.即Ⅰ(1).X(10).C(100).M(1000).V(5).L(50).D(500).记数的方法: 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3: 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8.

【leetcode】12. Integer to Roman

题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 解题思路: 注意到用例比较少,所以采用以空间换时间的方法,把所有的结果列出,然后组合出输入值n的字符串即可. 具体代码: 1 public class Solution { 2 public static String intToRoman(int num) { 3 St

12. Integer to Roman java solutions

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3: 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8:Ⅻ = 12: 小的数字,

LeetCode 12 Integer to Roman(整型数到罗马数)

翻译 给定一个整型数值,将其转换到罗马数字. 输入被保证在1到3999之间. 原文 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 我不会告诉你一开始我是用的无数个变量和if-- 后来实在受不了这么多变量就将其写成了枚举,那么接下来就迎刃而解了. 为了让大家理解罗马数是怎么计数的,这里我截了一张图,具体的大家可以自行用微软Bing

LeetCode 12 Integer to Roman(C,C++,Java,Python)

Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Solution: 根据数字将每一位转换为罗马字符串即可,时间复杂度O(len(num)) 题目大意: 给一个整数,将整数调整为罗马数字,关于罗马数字的定义见这里:罗马数字 个位数举例 Ⅰ,1 ]Ⅱ,2] Ⅲ,3] Ⅳ,4 ]Ⅴ,5 ]Ⅵ,6]Ⅶ,7] Ⅷ,8 ]Ⅸ

12. Integer to Roman 整数转罗马数字

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class Solution:     def intToRoman(self, num):         """     

12. Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. char* intToRoman(int num) { char* ret = malloc(sizeof(char)*13); //Each digit at maximum be written as 3 Roman character char* p = ret; char r