LeetCode 12. Integer to RomanLeetCode
整数转罗马数字
first submission
import math
class Solution:
def __init__(self):
self.roman={1:'I',5:'V',10:'X',50:'L',100:'C',500:'D',1000:'M'}
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
s=''
while num>0:
# number of digits -1
digits=int(math.log10(num))
# most high digits
e=(num//10**digits)
# num surplus
print(e,digits,end="")
if e>0:
#e==9 or e==4
num%=e*10**digits
print(':',s,num)
if e%5==4:
s+=self.roman[10**digits]
e+=1
if e%5==0:
s+=self.roman[e*10**digits]
e=0
digits=0
elif e>5:
s+=self.roman[5*10**digits]
e-=5
digits=0
if 0<e<=3:
if e>1:
s2=self.roman[1]*e
s+=s2
if digits:
s+=self.roman[10**(digits)]
return s
Wrong Answer:
Input:
1
Output:
""
Expected:
"I"
Input:
20
Output:
"IIX"
Expected:
"XX"
second submission
import time
import math
class Solution:
def __init__(self):
self.roman={1:'I',5:'V',10:'X',50:'L',100:'C',500:'D',1000:'M'}
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
s=''
while num>0:
# number of digits -1
digits=int(math.log10(num))
# most high digits
e=(num//10**digits)
# num surplus
print(e,digits,end="")
if e>0:
#e==9 or e==4
num%=e*10**digits
print(':',s,num)
if e%5==4:
s+=self.roman[10**digits]
e+=1
if e%5==0:
s+=self.roman[e*10**digits]
e=0
digits=0
elif e>5:
s+=self.roman[5*10**digits]
e-=5
#digits=0
if 0<e<=3 and digits==0:
s2=self.roman[1]*e
s+=s2
if digits:
s+=self.roman[10**(digits)]
if e>0:
e-=1
num+=e*10**digits
return s
if __name__ == "__main__":
data = [
{
"input":3,
"output":"III",
},
{
"input":4,
"output":"IV"
},
{
"input":9,
"output":"IX"
},
{
"input":58,
"output":"LVIII"
},
{
"input":1994,
"output":"MCMXCIV"
},
{
"input":1,
"output":"I"
},
{
"input":20,
"output":"XX"
},
{
"input":60,
"output":"LX"
}
];
for d in data:
print(d['input'])
# 计算运行时间
start = time.perf_counter()
result=Solution().intToRoman(d['input'])
end = time.perf_counter()
print(result)
if result==d['output']:
print("--- ok ---> spend time: ",end-start)
else:
print("--- error ---> spend time: ",end-start)
break
print()
else:
print("success")
总结:只是想个大概,然后就去做,遇到问题再改,头脑里没有一个全局观。惭愧惭愧。
LeetCode 12. Integer to RomanLeetCode的更多相关文章
- 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] 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 ☆☆
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [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 ...
- Java [leetcode 12] Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- [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
一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
随机推荐
- redis水平扩展实践,完全配置,无需代码改动
设计思路 思路很简单,就是基于用户ID进行分库,将用户的ID字符串按照byte逐个计算ID对应的hash原值(一个数字,取绝对值,因为原始值可能过大溢出,变成负数),然后,再用这个hash原值对库的个 ...
- 【java】注释
一.注释 1. 注释类型 [a]. 单行注释 // 单行注释 [b]. 多行注释 /* 多行注释 */ [c]. 文档注释 /** 文档注释 */ 一般情况下,需求,实现方式用多行注释,类和方法上 ...
- 修改machine.config遇到System.Net.ServicePointManager 的类型初始值设定项引发异常
<system.net>节点应该在</configuration>上面添加,即config页尾. 而不是在<configuration> 后面添加. 在</s ...
- IntelliJ IDEA maven项目 ***
创建一个新Maven项目 new 一个project 不选择任何Maven模板 起个GroupId.ArifactId 起个项目名.注意:Idea_Project是存放此项目的工作区间,mavenDe ...
- 找不到 EntityType “ ” 的映射和元数据信息。
意思是: 数据库里边有添加的新字段DB1,而程序中的的实体即“元数据”中没有这个新字段Et1,由于EntityFramework更新模型时已自动默认对DB1和Et1进行了映射(默认认为实体中存在这个字 ...
- C++进阶--派生类的析构(虚析构函数,shared_ptr)
//############################################################################ /* 在多态虚基类中声明一个虚析构函数 * ...
- Android开发教程之【环境搭建及HelloWorld】(转载)
引言 本系列适合0基础的人员,因为我就是从0开始的,此系列记录我步入Android开发的一些经验分享,望与君共勉!作为Android队伍中的一个新人的我,如果有什么不对的地方,还望不吝赐教. 在开始A ...
- P2837晚餐队列安排
传送 特写此篇,纪念不用dp做dp题 洛谷说这是个dp,但我不信(其实就是不会dp),因此我们考虑用另一种思路.修改后的队列每一个 数a[i]一定满足a[i]<=a[i+1],那修改后的顺序就是 ...
- OpenStack镜像服务基本操作
查询Glance服务状态 #glance-control all status [root@controller ~]# glance-control all status glance-api (p ...
- BGP属性+13条选路原则(转载)
原文:http://blog.sina.com.cn/s/blog_be409c2f0102x6sg.html BGP(Border Gateway Protocol)边界网关协议 BGP(Borde ...