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, 力扣, ...
随机推荐
- Docker镜像加速,设置国内源
源地址设置 在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件) { "registry-mirrors": [ "https ...
- Codeforces 1315B Homecoming (二分)
After a long party Petya decided to return home, but he turned out to be at the opposite end of the ...
- Atcoder Beginner Contest 147C(状态压缩)
最多15个人,用N个二进制的数字表示每个人的状态,然后检验.这串数字相当于已经把这些人的状态定了下来,如果和输入的情况不符则这串数字不正确,直接忽略,因为枚举了所有的情况,所以总有正确的,不必在错误的 ...
- Go文件拷贝
package main import ( "os" "io" "fmt" "io/ioutil" ) func mai ...
- 删除数据高级用法:delete,truncate
1.语法: delete 允许使用条件(删除符合条件的数据) 允许使用limit,限制删除的记录数.limit N 常见的是,limit配合order by来使用:先将结果排序,再删除固定数量 ...
- ALSA driver--HW Buffer
当app在调用snd_pcm_writei时,alsa core将app传来的数据搬到HW buffer(即DMA buffer)中,alsa driver从HW buffer中读取数据传输到硬件播放 ...
- Codeforces Round #577 (Div. 2) 题解
比赛链接:https://codeforc.es/contest/1201 A. Important Exam 题意:有\(n\)个人,每个人给出\(m\)个答案,每个答案都有一个分值\(a_i\), ...
- 本次我们使用idea构建springmvc项目
该案例的github地址:https://github.com/zhouyanger/demo/tree/master/springmvcdemo1 1.首先我们可以创建maven项目,file-&g ...
- [JavaScript] 两个数相除有余数时结果加1
实现代码 ; ; ?(total/item):(Math.floor(total/item)+); console.log(page)
- PS——"火龙"
一.打开素材龙图案(因为老师给了素材,所以直接打开了) 二.Ctrl+J把背景图层复制一份,用魔棒工具把白色的背景去除(背景和龙的外围会出现蚂蚁线),Delete键清除,Ctrl+D取消选择 三.全选 ...