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[] = {"", ...
随机推荐
- 安装tensorflow,那叫一个坑啊
最近,项目团队需要研究并应用AI的技术,在具体的产品实施环节中使用.之前的几个项目,是委托武汉大学给做的,基于keras框架,实现了一些图像识别的项目. 这不,上方希望自己能够掌握一些常用且成熟的AI ...
- excel技巧--文本拆分合并
如果像上图那样将一单元格内拆分成同等大小的字词,可用如下步骤: 1.将该单元格的宽度缩至拆分词的大小: 2.选择同列的适当的单元格,用于填充拆分的字符: 3.点击“开始”-->填充-->两 ...
- HelloStruts
Hello Struts 建立项目 打开myeclipse 新建项目 选择myeclipse-java enterprise projects-web project 项目名称:hellostruts ...
- 胖子哥的大数据之路(7)- 传统企业切入核心or外围
一.引言 昨天和一个做互联网大数据(零售行业)的朋友交流,关于大数据传统企业实施的切入点产生了争执,主要围绕两个问题进行了深入的探讨: 问题1:对于一个传统企业而言什么是核心业务,什么是外围业务? 问 ...
- java反射以及动态代理的学习
java反射学习 1)字节码文件的三种获取方式 ①:Object类的getClass()方法:对象.getClass() ②:数据类型的静态的class属性:类名.class ③:通过Class类的静 ...
- STL基础--仿函数(函数对象)
1 首先看个仿函数的例子 class X { public: void operator()(string str) { // 函数调用运算符,返回类型在operator之前 cout << ...
- sklearn.cross_validation 0.18版本废弃警告及解决方法
转载:cheneyshark 机器环境: scikit-learn==0.19.1 Python 2.7.13 train_test_split基本用法 在机器学习中,我们通常将原始数据按照比例分割为 ...
- docker entrypoint入口文件详解
docker entrypoint入口文件详解 pasting Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较 [k8s]args指令案例-彻底理解docke ...
- MySQL事务提交过程(转载)
http://blog.csdn.net/sofia1217/article/details/53968214 上一篇文章我们介绍了在关闭binlog的情况下,事务提交的大概流程.之所以关闭binlo ...
- Linux上面的MTU含义
问题场景描述: 最近在搞一个很菜的程序--FTP上传文件. 但是这个 很菜的程序搞的我脑袋疼了 半个月. 在linux上面部署了我的程序,上传文件在1KB以下顺利上传,但是1KB以上上传不上去. 程序 ...