[LeetCode]题解(python):013-Roman to Integer
题目来源:
https://leetcode.com/problems/roman-to-integer/
题意分析:
这道题目和上一题目相反,是将罗马数字转化成阿拉伯数字。
题目思路:
只要知道罗马数字和阿拉伯数字之间是怎么转换的就可以了。先做一个字符和数值对应的字典,{'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000};如果发现输入的字符串后一位比前一位小,则是出现4,9之类的,那么将前一个字符对应的数值减去两次就可以了。
代码(python):
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
ans = 0
size = len(s)
i = 0
while i < size:
if i > 0 and d[s[i]] > d[s[i - 1]]:
ans += d[s[i]] - 2 * d[s[i - 1]]
else:
ans += d[s[i]]
i += 1
return ans
转载请注明出处:http://www.cnblogs.com/chruny/p/4817835.html
[LeetCode]题解(python):013-Roman to Integer的更多相关文章
- [LeetCode 题解]: Interger to Roman
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an i ...
- 【LeetCode算法-13】Roman to Integer
LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- LeetCode--No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- 【LeetCode】013. Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 【JAVA、C++】LeetCode 013 Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [Leetcode]013. Roman to Integer
public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; ...
- leetcode第13题--Roman to Integer
Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
随机推荐
- BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛
题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec Memory Limit: 64 MB Description FJ's N ( ...
- Android开发匹配字符笔记
Windows下的回车换行符是\r\n,而Linux下的回车换行符是\n 所以,在windows下可以用\r\n,而在linux下要用\n 并且还发现在linux下(既在android上开发)需要匹配 ...
- HDU 4464 Browsing History(最大ASCII的和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4464 Problem Description One day when you are going t ...
- 《网络编程》Unix 域套接字
概述 Unix 域套接字是一种client和server在单主机上的 IPC 方法.Unix 域套接字不运行协议处理,不须要加入或删除网络报头,无需验证和,不产生顺序号,无需发送确认报文,比因特网域套 ...
- mysql安装详细步骤图解
本文转自http://blog.csdn.net/fanyunlei/article/details/21454645 别看图多,其实mysql的安装十分简单,一路next即可,只是注意倒数第三步,设 ...
- Android应用开发基础篇(13)-----GestureDetector(手势识别)
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/05/2381025.html 一.概述 GestureDetector是一个用于识别手势的类,这 ...
- 空类的默认函数—— SAP电面(2)/FEI
定义一个空类 class Empty { }; 默认会生成以下几个函数 2. 拷贝构造函数 Empty(const Empty& copy) { } 3. 赋值运算符 Empty& o ...
- String VS Cstring(字符串)
#include<string> 与 #include<string.h> 这是两个完全不同的头文件,前者用于C++,后者用于C,一般把这两个头文件都包括进去. 越来越觉得需要 ...
- 数组length属性的一些特性
~~·数组的length属性是可读写的 var colors = ["blue","red","green"];colors.length ...
- mget 同时获取
mget 同时获取: http://192.168.32.81:9200/ _mget POST { "docs" :[ { "_index":"li ...