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[] = {"", ...
随机推荐
- Python Scrapy环境配置教程+使用Scrapy爬取李毅吧内容
Python爬虫框架Scrapy Scrapy框架 1.Scrapy框架安装 直接通过这里安装scrapy会提示报错: error: Microsoft Visual C++ 14.0 is requ ...
- java (图片转PDF)
1.导入jar包 itextpdf-5.5.12.jar 2.写代码 package com.util; import java.io.File; import java.io.FileNotFoun ...
- Spring Cloud 快速教程
官方:http://projects.spring.io/spring-cloud/ 中文:https://springcloud.cc/ https://blog.csdn.net/forezp/a ...
- hyperledger fabric各类节点及其故障分析 摘自https://www.cnblogs.com/preminem/p/8729781.html
hyperledger fabric各类节点及其故障分析 1.Client节点 client代表由最终用户操作的实体,它必须连接到某一个peer节点或者orderer节点上与区块链网络通信.客户端 ...
- [转]happybase1.0 报错:ThriftPy does not support generating module with path in protocol 'f'
happybase1.0 报错:ThriftPy does not support generating module with path in protocol 'f' 2016-10-12 14: ...
- 5分钟搭建 nginx +php --------------(LNMP)新手专用
5分钟搭建 nginx +php --------------(LNMP)新手专用 2014-11-14 16:48 88876人阅读 评论(2) 收藏 举报 版权声明:本文为博主原创文章,未经博主允 ...
- WebService C#开发/调用
简单描述C#开发WebService操作步骤以及调用方式 WebService开发 第一步:创建Web空项目 第二步:为创建的Web空项目添加Web服务 第三步:实现WebService方法(仅供参考 ...
- mysqlli 的基本用法
Mysqli是php5之后才有的功能 需要修改php.ini的配置文件 查找下面的语句: ;extension=php_mysqli.dll 将其修改为:extension=php_mysqli.dl ...
- PHP echo()、print()、print_r()、var_dump()、var_export()的区别
PHP中echo.print.print_r.var_dump.var_export的用法与区别 这些均为输出变量的内容: echo();不是函数.是PHP语句.没有返回值:输出一个或者多个字符串或者 ...
- 18年春招某编程题:有三个整数X,Y,Z,要求进行若干次操作使得X,Y,Z相等
题目描述: 给定三个整数X,Y,Z,要求进行若干次操作使得X,Y,Z相等,操作有两种: 1.从X,Y,Z中选择两个数都加1. 2.从X,Y,Z中选择一个数加2. 求最少需要多少次操作. 题目思路: 1 ...