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[] = {"", ...
随机推荐
- 自然语言处理之:搭建基于HanLP的开发环境(转)
环境搭建比FNLP的简单,具体参考:https://github.com/hankcs/HanLP 各个版本的下载:https://github.com/hankcs/HanLP/releases 完 ...
- php上传导入文件 nginx-502错误
4. php程序执行时间过长而超时,检查nginx和fastcgi中各种timeout设置.(nginx 中的 fastcgi_connect_timeout 300;fastcgi_send_ti ...
- 【巷子】---middleware---redux-promise-middleware---【react】
一.Middleware的由来 单一的state是存储在store中,当要对state进行更新的时候,首先要发起一个action(通过dispatch函数),action的作用就是相当于一个消息通知, ...
- DS树+图综合练习--带权路径和
题目描述 计算一棵二叉树的带权路径总和,即求赫夫曼树的带权路径和. 已知一棵二叉树的叶子权值,该二叉树的带权案路径和APL等于叶子权值乘于根节点到叶子的分支数,然后求总和.如下图中,叶子都用大写字母表 ...
- HTML+CSS补充
1. HTML+CSS补充 - 布局: <style> .w{ width:980px;margin:0 auto; } </style> <body> <d ...
- 【SQL Server】MS SQL Server中的CONVERT日期格式化大全
CONVERT 函数将某种数据类型的表达式显式转换为另一种数据类型.SQL Server中 将日期格式化. SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式. 在表中,左侧的两列表示将 ...
- PAT 乙级 1036 跟奥巴马一起编程(15) C++版
1036. 跟奥巴马一起编程(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 美国总统奥巴马不仅呼吁所有人 ...
- 切换了webview 定位不了的解决方法 (还没有试,记录在此)
# 切换到 webview time.sleep(2) print(driver.contexts) driver.switch_to.context('WEBVIEW_com.tencent.mm: ...
- HTTP API网关选择之一Kong介绍
为什么需要 API 网关 在微服务架构之下,服务被拆的非常零散,降低了耦合度的同时也给服务的统一管理增加了难度.如上图左所示,在旧的服务治理体系之下,鉴权,限流,日志,监控等通用功能需要在每个服务中单 ...
- JVM异常之:堆溢出OutofMemoryError
1.堆溢出 Java 堆内存的OutOfMemoryError异常是实际应用中最常见的内存溢出异常情况.出现Java 堆内存溢出时,异常堆栈信息“java.lang.OutOfMemoryError” ...