博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode By Python]13 Roman to Integer
阅读量:4055 次
发布时间:2019-05-25

本文共 760 字,大约阅读时间需要 2 分钟。

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

这里说一下罗马数的简单规则,左边的数>右边的数时,两个数相加,左边的数<右边的数时,右边的数减去左边的数,如

"DCXXI"  =  621
romanToint = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}  #罗马数与整数之间的转换关系
class Solution(object):    def romanToInt(self, s):        """        :type s: str        :rtype: int        """        romanToint = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}        xlength = len(s)        results = 0        for a in range(1,xlength):            if romanToint[s[a]]>romanToint[s[a-1]]:                results = results-romanToint[s[a-1]]            else:                results = results+romanToint[s[a-1]]        results = results+romanToint[s[-1]]        return results

转载地址:http://lwhci.baihongyu.com/

你可能感兴趣的文章
【opencv学习笔记】012之形态学操作(开闭操作,形态学梯度,顶帽与黑帽)
查看>>
【CryptoZombies - 1 Solidity 教程】011 Require
查看>>
【CryptoZombies - 1 Solidity 教程】012 继承(Inheritance)和 引用 (import)
查看>>
【CryptoZombies - 1 Solidity 教程】013 永久存储变量(storage)和 临时存储变量(memory)
查看>>
【opencv学习笔记】013之形态学操作应用(trackbar应用)
查看>>
【CryptoZombies - 1 Solidity 教程】014 函数可见性
查看>>
【CryptoZombies - 1 Solidity 教程】015 接口interface
查看>>
【opencv学习笔记】014之上采样与降采样
查看>>
【opencv学习笔记】015之基本阈值操作
查看>>
【CryptoZombies - 1 Solidity 教程】016 函数多返回值&奖励实战
查看>>
【CryptoZombies - 2 Solidity 进阶】001 智能合约的不可篡改性与Ownable
查看>>
【积跬步以至千里】App Crashed - WriteMiniDump
查看>>
我努力是因为, 我想通过自己,带给这个世界点什么!
查看>>
数据结构基础笔记、基础知识总结、周周练汇总,通过代码,更快速掌握数据结构和算法知识!
查看>>
赛前必看!!NOIP竞赛及CSP认证初赛赛前辅导详细视频教程!!!
查看>>
完美解决AttributeError: module ‘torchvision.models‘ has no attribute ‘detection‘
查看>>
VMWare报错:无法获得VMCI驱动程序的版本:句柄无效。
查看>>
重磅!AI与区块链技术知识分享交流会!特邀贾志刚老师、双一流211高校研究生!
查看>>
入门卷积神经网络必备,基础、理论、实战一网打尽!
查看>>
Java报错:No enclosing instance of type learnJ is accessible.
查看>>