整数转罗马数字

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的更多相关文章

  1. Leetcode 12——Integer to Roman

    12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...

  2. Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. Java [leetcode 12] Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  7. [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 ...

  8. LeetCode——12. Integer to Roman

    一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...

  9. LeetCode 12 Integer to Roman (整数转罗马数字)

    题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description   String M[] = {"", ...

随机推荐

  1. spring-jar包及架构介绍

    查看博客: http://www.cnblogs.com/ywlaker/p/6136625.html

  2. 【java】构造函数

    什么时候自定义构造函数:当分析事物时,事物一存在就具备一些特征或者行为,那么就把这么内容定义在构造函数中 作用:对对象进行初始化,对象一建立,就会自动调用与之对应的构造函数 特点: 1.函数名和类名相 ...

  3. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll 1..BeautifulSoup库的使用 Beautiful ...

  4. 帆软:不使用 __parameters__ 传参,问题。

    原帖地址:http://bbs.fanruan.com/thread-117614-1-1.html 在设计器本地可以用下面方式传参数,这个方式跟 &userID=28 的结果是一样的. Re ...

  5. 分页sql写法【只用最新的】

      offset m rows   FETCH NEXT n ROWS ONLY m=(pageindex-1)*pagesize n=pagesize sql server 2012以上适用.

  6. 简单说throw和throws的区别

    1. 区别 throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是指抛出的一个具体的异常类型. 2.分别介绍 ...

  7. OpenSSH多路复用Multiplexing配置

    设置 Session Multiplexing 在客户端节点如下配置/etc/ssh/ssh_config 或~/.ssh/config 就可以直接开启 Session Multiplexing 功能 ...

  8. [转][JS]修改链接中的参数

    转自:https://blog.csdn.net/weixin_40845192/article/details/81561644 /** * url地址修改 * @param url 待修改url ...

  9. 如何做适合seo的404页面

    我补充一点,404页面对于seo来说也是比较重要的,之所以不让跳转到首页就是楼上说的,容易被误判,所以,一般404页面的作用是引导客户点击进入首页. 实践证明,做了比较好的404页面对网站整体流量和排 ...

  10. 用JavaScript来生成HTML

    用JavaScript来生成HTML <style> table{ border-top: 1px #ff0000 solid; border-left: 1px #ff0000 soli ...