leetcode python3 简单题13. Roman to Integer
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第十三题
(1)题目
英文:
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
中文:
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-parentheses
(2)解法
① 使用dict.get(耗时:64ms,内存:13.6M)
class Solution:def romanToInt(self, s: str) -> int:dic = {'I':1, 'IV':4, 'V':5, 'IX':9, 'X':10, 'XL':40, 'L':50, 'XC':90, 'C':100, 'CD':400, 'D':500, 'CM':900, 'M':1000}return sum(dic.get(s[max(i-1, 0):i+1], dic[n]) for i, n in enumerate(s))
注意:
1.这里的dict是将所有的单字符,双字符组合的情况全部考虑了,如果不考虑双字符组合,那么程序会变得非常复杂,需要很多判断语句
② 双循环方法(耗时:156ms,内存:13.7M)
class Solution:def romanToInt(self, s: str) -> int:dic = {'IV':4, 'IX':9,'XL':40,'XC':90,'CD':400,'CM':900,'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}flag=0res = 0lens = len(s)for key, val in dic.items():for i, char in enumerate(s):lenkey = len(key)if i<lens and s[i:i+lenkey]==key:if lenkey==2:flag=1i+=lenkeyres+=valif lenkey==2 and flag==1:res-=sum([dic.get(i) for i in (key)])flag=0return res
注意:
1.最关键的点是flag的设置,因为dic和s的双循环存在,所以会造成dict中两个字符的组合会多算两次单个字符的和,所以要减去。
2.dic.items()这样枚举key-value对的时候是有顺序的,顺序就是dic中排列的顺序。
3.in enumerate会自动生成index,value,无论最后是否输出index,如果不给index留位置,则输出的是tuple类型(index,value)
4.str变量可以用for in的结构来得到每一个单一字符。
leetcode python3 简单题13. Roman to Integer相关推荐
- leetcode python3 简单题7.Reverse integer
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七题 (1)题目 英文: Given a 32-bit signed intege ...
- leetcode python3 简单题70. Climbing Stairs
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...
- leetcode python3 简单题69. Sqrt(x)
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第六十九题 (1)题目 英文: Implement int sqrt(int x). ...
- leetcode python3 简单题231. Power of Two
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十一题 (1)题目 英文: Given an integer, write ...
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题202. Happy Number
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零二题 (1)题目 英文: Write an algorithm to det ...
- leetcode python3 简单题191. Number of 1 Bits
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十一题 (1)题目 英文: Write a function that ta ...
- leetcode python3 简单题190. Reverse Bits
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百九十题 (1)题目 英文: Reverse bits of a given 3 ...
- leetcode python3 简单题172. Factorial Trailing Zeroes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...
最新文章
- SharePoint 2013 图文开发系列之创建内容类型
- vs2019c语言头文件的路径,vs2019设置及第三方库的使用,
- 徐教授的对于商业模式创新的讲座!十分有用!
- python基础——注释、字符串、输出换行
- 【Redis系列】面试官:Redis中的数据已经过期,为什么还占用这内存?
- 争做RTC领域标杆——与华为云副总裁薛浩聊聊我们的视频时代
- 数据结构--双链表的创建和操作
- C++难吗?好学吗?C++到底怎么样?
- 你绝没用过的一款高逼格空间可视化工具
- 程序猿崛起2——互联网时代下的新潮流和新活法
- 巧用 Dummy 解决断网情况下的网络访问问题
- 零基础如何搭建个人网站,附完整建站步骤!
- 太白---落燕纷飞第一重 Android单元测试Instrumentation和irobotium
- 2018年兰博基尼突破5000台销量大关
- 微信小程序如何隐藏右上角分享按钮
- 初入红尘不知人间疾苦
- 手把手教你5G时代Webview的正确使用姿势,小白也能看明白
- 腾讯2021校园招聘技术类编程题汇总
- 后疫情时代,VR购物—零售业的硬核破局之道
- 嘀嘀打车、快的打车该醒醒了
热门文章
- php连接mysql数据库输出_PHP连接并输出数据库数据实例代码
- 数据结构 2-0 线性表总结
- nlp-tutorial代码注释3-1,RNN简介
- 正态分布初始化 torch.nn.Embedding.weight()与torch.nn.init.normal()的验证对比
- Java中List判空问题白话详解
- ViewPager——PagerTitleStrip和PagerTabStrip
- vue如何保存登录状态到全局?【vue状态管理】
- 002 Ajax中传输格式为HTML
- 淘宝爬取某人的所有购物订单
- Delphi的子类化控件消息, 消息子类化