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[] = {"", ...
随机推荐
- hadoop append 追加文件错误
java.io.IOException:Failed to replace a bad datanode on the existing pipeline due to no more good da ...
- 使用shell脚本批处理控制大数据环境服务启动停止
三台集群机器: master 192.168.168.200 slave1 192.168.168.201 slave2 192.168.168.202 1.start-maste ...
- 解决 VUE 微信登录验证 【感谢原文:https://segmentfault.com/a/1190000009493199】
[感谢原文:https://segmentfault.com/a/1190000009493199] SPA单页应用中微信授权登录的一点思路 单页应用应该如何解决微信授权登录的尴尬跳转?后退无法返回? ...
- FLIR ONE PRO热成像仪
FLIR ONE PRO热成像仪 https://www.chiphell.com/thread-1774218-1-1.html
- TextBox限制输入字母、数字、退格键
公共方法如下: /// <summary> /// 正则表达式验证只能输入数字或字母 /// </summary> /// <param name="pendi ...
- ManualResetEvent学习实例
ManualResetEvent为多个线程之间提供了一个共享的信号. 初始化:ManualResetEvent mre=new ManualResetEvent(true) 初始值为true表示有信号 ...
- Tribon/AM 数据库名字的含义
收集在这里备用 数据库名 Tribon环境变量 内容描述 船体型线数据库 SB_CGDB 船体外型信息 型表面,船体曲线,板缝 船体结构数据库 SB_OGDB 船体模型信息 板材数据库 SB_PLDB ...
- Netty Tutorial Part 1.5: On Channel Handlers and Channel Options [z]
Intro: After some feedback on Part 1, and being prompted by some stackoverflow questions, I want to ...
- QTreeWidget的Item点击事件
转载:cw123458945 #!/usr/bin/env python import sys from PyQt4.QtCore import SIGNAL from PyQt4.QtGui imp ...
- C#DateTime好用但不常用的用法
获取某年的某一个月天数 DateTime.DaysInMonth(year, i);