13. Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
相比Interger to Roman,本题明显要简单一些,按照规则直接翻译就好。
AC代码:
class Solution(object):
def romanToInt(self, s):
roman_to_int = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
i, num = 0, 0
while i < len(s) - 1:
if roman_to_int[s[i]] >= roman_to_int[s[i + 1]]:
num += roman_to_int[s[i]]
else:
num -= roman_to_int[s[i]]
i += 1
num += roman_to_int[s[i]]
return num
13. Roman to Integer的更多相关文章
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- C# 写 LeetCode easy #13 Roman to Integer
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【LeetCode】13. Roman to Integer (2 solutions)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- 13. Roman to Integer[E]罗马数字转整数
题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
随机推荐
- 纯代码利用CSS3 圆角边框和盒子阴影 制作 iphone 手机效果
原文:纯代码利用CSS3 圆角边框和盒子阴影 制作 iphone 手机效果 大家好,我是小强老师. 今天我们看下CSS3最为简单的两个属性. css3给我们带来了很多视觉的感受和变化,以前的图片做的事 ...
- localstroge与cookie的区别
HTML5本地存储是一种让网页可以把键值对存储在用户浏览器客户端的方法.像Cookie一样,这些数据不会因为你打开新网站,刷新页面,乃至关闭你的浏览器而消失. 而与Cookie不同的时,这些数据不会每 ...
- 【解决方法】You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE)
出现场景: 正常调试是没有问题的,但是在Archive的时候,报出了这个错误. 问题详情: (null): URGENT: all bitcode will be dropped because ‘x ...
- [每日一题] OCP1z0-047 :2013-08-22 正则表达式---[^Ale|ax.r$]'
正确答案:DE 一.Oracle正则表达式的相关知识点 '[^Ale|ax.r$]': ^:匹配行的开始字符 $:匹配行的结束字符 []:方括号表示指定一个匹配列表,该列表匹配列表中显示的任何表达式. ...
- 理解Android的layout和measure
在Android UI开发中,总会有情况需要自定义View和View Group. 什么是View?就是Android中一个基本视图单位,一个Button是一个view, 一个Layout, 也是一个 ...
- cookieless domain
概述 什么是cookieless domain?虽然名字中带有cookie,其实完全可以不使用cookie.这只是一种将网页中静态的文本,图片等的域名和主域名相区别开的方法. 主域名难免会使用到coo ...
- ExtJS003单击按钮弹出window
html部分 <input type="button" id="btn" name="name" value="点击&quo ...
- Mac环境下Myeclispe2015工具的安装与破解
链接地址:http://www.07net01.com/2015/08/919753.html 07net01.com 发布于 2015-08-30 22:19:37 分类:IT技术 阅读(306) ...
- [LeetCode]题解(python):005-Longest Palindromic Substring
题目来源: https://leetcode.com/problems/longest-palindromic-substring/ 题意分析: 这道题目是输入一段不超过1000的字符串,输出最长的回 ...
- HTML+CSS笔记 CSS入门续集
继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代(标签). 语法: p{color:red;} <p> ...