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 : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 12. Integer to Roman(自己想的复杂的算法).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))

3、大神的解法

LeetCode上看到的大神的解法,实在是佩服! 采用字典将特殊符号记住,随后逐一判断.......

 class Solution:
def intToRoman(self, num: int) -> str:
res = ""
s = 1000 d = {1: "I", 5: "V", 10: "X", 50: "L", 100: "C", 500: "D", 1000: "M"}
while num != 0:
r, temp = divmod(num, s) if r == 9:
res += d[s] + d[s * 10]
elif r == 4:
res += d[s] + d[s * 5]
elif r >= 5:
res += d[s * 5] + d[s] * (r - 5)
else:
res += d[s] * r s = s // 10
num = temp return res print(Solution().intToRoman(2964))
												

leetCode练题——12. Integer to Roman的更多相关文章

  1. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  2. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  3. 乘风破浪:LeetCode真题_007_Reverse Integer

    乘风破浪:LeetCode真题_007_Reverse Integer 一.前言 这是一个比较简单的问题了,将整数翻转,主要考察了取整和取余,以及灵活地使用long型变量防止越界的问题. 二.Reve ...

  4. 《LeetBook》leetcode题解(12):Integer to Roman[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. 【LeetCode】12. Integer to Roman (2 solutions)

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  6. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  7. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  8. [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 ...

  9. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

随机推荐

  1. IntelliJ IDEA 2017.3尚硅谷-----设置界面

  2. Linux常用命令英文全称与中文解释 (pwd、su、df、du等)

    https://blog.csdn.net/qq_40334837/article/details/83819735 Linux常用命令英文全称与中文解释 apt: Advanced Packagin ...

  3. WLC-Virtual Interface IP

    关于思科WLC,有很多接口类型,如下所示,这里主要针对Virtual IP记录一些最佳实践建议. 思科WLC的Virtual IP地址的作用: • Mobility management • DHCP ...

  4. Go语言标准库flag基本使用

    文章引用自   Go语言标准库flag基本使用 os.Args 如果你只是简单的想要获取命令行参数,可以像下面的代码示例一样使用os.Args来获取命令行参数. package main import ...

  5. ELK节点安装

    ELK 安装参考链接 https://www.cnblogs.com/xialiaoliao0911/p/9599898.html setenforce 0sed -i s/enforcing/dis ...

  6. 【Python】第一个程序---Helloworld!

    对于大多数程序语言,第一个入门编程代码便是"Hello World!",以下代码为使用Python输出"Hello World!": #!/usr/bin/py ...

  7. VMware下Linux虚拟机访问本地Win共享文件夹

    VMware下Linux虚拟机访问本地Win共享文件夹 : 访问共享文件夹的步骤如下: 1.先在本地Win创建一个需要共享到虚拟机中的共享目录,如  F:\share. 2.打开你的VMware,选择 ...

  8. quernation,euler,rotationmatrix之间的相互转换

    转自:https://blog.csdn.net/zhuoyueljl/article/details/70789472

  9. opencv:截取 ROI 区域

    Rect roi; roi.x = 100; roi.y = 100; roi.width = 250; roi.height = 200; // 截取 ROI 区域 // 这种方式改变 sub,原图 ...

  10. ant+jmeter 自动生成测试报告

    1,把Jmeter根目录/extras 下的ant-jmeter-xxx.jar拷贝到ant根目录/lib下 2, 修改Jmeter的bin目录下jmeter.properties文件的配置:jmet ...