整数转罗马数字

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. java -jar 执行jar包出现 java.lang.NoClassDefFoundError

    我用idea工具将自己开发java程序打成一个可执行的jar包,当然用eclipse或者直接用jar命令行都无所谓,本质都是将程序归档到一个压缩包,并附带一个说明清单文件. 打jar的操作其实很简单, ...

  2. 【转】解决ubuntu13.10下,无法双击运行脚本文件

    解决ubuntu13.10下,无法双击运行脚本文件 转自:http://www.aichengxu.com/other/975350.htm    首先,必须先设定好脚本的运行方法,当然如果只是she ...

  3. jquery位置问题

    在页面中添加jquery时,一定要把jquery放在其他js文件前面!不然会出现"$ is not defined", 导致js无法正常运行.

  4. 服务器病了吗? Linux 服务器的那些性能参数指标

    一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤其当你的程序非正常工作的时候, ...

  5. 解决Windows远程桌面连接每次都提示输入密码的问题,远程桌面记不住密码

    FROM:http://www.veryhuo.com/a/view/80444.html Windows 远程桌面连接几乎每天都用,所以使用的方便性非常重要.如果你经常用,也许会发现在某些系统中,每 ...

  6. DS二叉树--Huffman编码与解码

    题目描述 1.问题描述 给定n个字符及其对应的权值,构造Huffman树,并进行huffman编码和译(解)码. 构造Huffman树时,要求左子树根的权值小于.等于右子树根的权值. 进行Huffma ...

  7. java设计模式-State模式

    1.背景: MM的状态是非常不固定的,说不定刚才还非常高兴,没准一会就生气了.就跟六月的天似的,说变就变. 封装一下MM的状态:smile,cry,say:MM的状态决定了这些方法该怎么执行.   2 ...

  8. 【ZZ】C++11之统一初始化语法 | 桃子的博客志

    C++11之统一初始化语法 | 桃子的博客志 https://taozj.net/201710/list-initialize.html 在当前新标准C++11的语法看来,变量合法的初始化器有如下形式 ...

  9. PHP7.1扩展开发入门

    第1步: 首先从官网下载了PHP源码http://am1.php.net/distributions/php-7.1.3.tar.bz2 第2步: 解压后可以看到根目录下面的ext文件夹里有ext_s ...

  10. echo() print() printf() print_r() 的区别

    echo是一个语言结构而非函数,因此它无法被变量函数调用, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print()    只能打印出简单类型变量的值(如int ...