leetCode练题——12. Integer to Roman
1、题目
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 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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(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的更多相关文章
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- 乘风破浪:LeetCode真题_007_Reverse Integer
乘风破浪:LeetCode真题_007_Reverse Integer 一.前言 这是一个比较简单的问题了,将整数翻转,主要考察了取整和取余,以及灵活地使用long型变量防止越界的问题. 二.Reve ...
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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 ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- [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 ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
随机推荐
- 计算机网络 - TCP_NODELAY 和 TCP_CORK, TCP_NOPUSH
参考 https://www.cnblogs.com/biyeymyhjob/p/4670502.html https://stackoverflow.com/questions/3761276/wh ...
- (DFS)HDU_1241 Oil Deposits
HDU_1241 Oil Deposits Problem Description The GeoSurvComp geologic survey company is responsible f ...
- TCP/IP详解,卷1:协议--1
引言 很多不同的厂家生产各种型号的计算机,它们运行完全不同的操作系统,但 T C P / I P 协议 族允许它们互相进行通信.这一点很让人感到吃惊,因为它的作用已远远超出了起初的设想. T C P ...
- 抽象类和final修饰符
抽象类和抽象方法的特点: 1.都通过abstract关键字来修饰. 2.抽象类不能实例化. 3.抽象类中可以有 0~多个抽象方法.(可以没有抽象方法) 4.抽象方法只有方法声明,没有方法实现.(没有方 ...
- Type Java类型
参考:https://blog.csdn.net/a327369238/article/details/52621043 Type —— Java类型 Type是一个空接口,所有类型的公共接口(父接口 ...
- 吴裕雄 python 机器学习——集成学习AdaBoost算法回归模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...
- 查询数据操作:limit
1.作用: 在查看数据时用于限制获得的记录数量,一般放在最后. 2.语法: limit offset,row_count; 解析: offset:偏移量,索引值默认从0开始,可以省略 row_coun ...
- eclipse修改快捷键
eclipse修改快捷键 ctrl + shift + l查看快捷键 window -> preferences -> 搜索keys 鼠标点击以下五个表头,可以按照内容搜索 示例,选中Bi ...
- AbstractQueuedSynchronizer(AQS) 超详细原理解析
java.util.concurrent包中很多类都依赖于这个类AbstractQueuedSynchronizer所提供的队列式的同步器,比如说常用的ReentranLock,Semaphore和C ...
- JVM系列(二)之类加载
什么是类的加载 类加载是指将源代码编译后的.class加载到内存中初始化待程序使用的过程,类加载的最终结果就是将.class字节码加载到JVM中生成一个java.lang.Class对象,提供给程序使 ...