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):

        """

        :type num: int

        :rtype: str

        """

        I = [‘‘, ‘I‘, ‘II‘, ‘III‘, ‘IV‘, ‘V‘, ‘VI‘, ‘VII‘, ‘VIII‘, ‘IX‘]

        X = [‘‘, ‘X‘, ‘XX‘, ‘XXX‘, ‘XL‘, ‘L‘, ‘LX‘, ‘LXX‘, ‘LXXX‘, ‘XC‘]

        C = [‘‘, ‘C‘, ‘CC‘, ‘CCC‘, ‘CD‘, ‘D‘, ‘DC‘, ‘DCC‘, ‘DCCC‘, ‘CM‘]

        M = [‘‘, ‘M‘, ‘MM‘, ‘MMM‘]

        thousand = num // 1000

        houndred = (num % 1000) // 100

        ten = (num % 100) // 10

        digit = num % 10

        return M[thousand] + C[houndred] + X[ten] + I[digit]

s = Solution()

# num = 1

# num = 19

# num = 99

# num = 100

num = 409

#num = 1999

res = s.intToRoman(num)

print(res)

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8445811.html

时间: 2024-10-10 01:35:48

12. Integer to Roman 整数转罗马数字的相关文章

[LeetCode]12. Integer to Roman整数转罗马数字

Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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 i

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,5,10.  1到3之间用重复的1表示,4用IV 90用XC 依次类推. I = 1; V = 5; X = 10; L = 50; C = 1

12 Integer to Roman(int转罗马数字Medium)

题目意思:1-3999转罗马数字 思路:从大往小减 ps:这题有点蛋疼 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 string a[]={"I","IV","V","IX","X","XL","L","XC","C","CD",

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 && 13. Roman to Integer && 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

leedCode练题——12. Integer to Roman

1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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 i

LeetCode 12 Integer to Roman (整数转罗马数字)

题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", "M", "MM", "MMM”};//1000~3000String C[] = {"", "C", "CC", "CCC", "CD", "D&quo

[LintCode] Integer to Roman 整数转化成罗马数字

Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range from 1 to 3999. Have you met this question in a real interview? Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals https:/

Leetcode12--->Integer to Roman(整数转换为罗马数字)

题目: 给定一个整数,将其转换为罗马数字; 题目很简单,主要是依靠整数和罗马数字的对应表: I= 1:V= 5: X = 10: L = 50: C = 100: D = 500: M = 1000 代码如下: 1 public class Solution { 2 public String intToRoman(int num) { 3 if(num <= 0) 4 return ""; 5 String[][] RomanDict = new String[][] { 6