题目来源:

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的更多相关文章

  1. [LeetCode 题解]: Interger to Roman

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an i ...

  2. 【LeetCode算法-13】Roman to Integer

    LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...

  3. No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

  4. LeetCode--No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

  5. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  6. 【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 ...

  7. 【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 ...

  8. [Leetcode]013. Roman to Integer

    public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; ...

  9. leetcode第13题--Roman to Integer

    Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  10. LeetCode记录之13——Roman to Integer

    能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...

随机推荐

  1. HDU 5763 Another Meaning(FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...

  2. 【转载】Android开源:数据库ORM框架GreenDao学习心得及使用总结

    转载链接:http://www.it165.net/pro/html/201401/9026.html 最近在对开发项目的性能进行优化.由于项目里涉及了大量的缓存处理和数据库运用,需要对数据库进行频繁 ...

  3. jquery 使用ajax调用c#后台方法

    $.ajax({                         type: "get",                         cache: false,        ...

  4. 10491 - Cows and Cars

    描述:要么全选择牛,要么选择一辆车和p-1头牛,那么剩下n+m-p道门可以选择,求选择p道门以后要选择到车的概率 #include <cstdio> int main() { //freo ...

  5. 模式匹配-KMP算法

    /***字符串匹配算法***/ #include<cstring> #include<iostream> using namespace std; #define OK 1 # ...

  6. 《Linux内核设计与实现》内存管理札记

    1.页 芯作为物理页存储器管理的基本单元,MMU(内存管理单元)中的页表,从虚拟内存的角度来看,页就是最小单位. 内核用struct page结构来标识系统中的每个物理页.它的定义例如以下: flag ...

  7. linux学习历程

    1.linux初步介绍:2.linux的第一次接触:3.linux用户管理4.linux常用命令(3600+个).5.linux下所有者,所在组和其他组的介绍6.linux下文件和目录权限机制 lin ...

  8. iframe跨域通信方案

    概述 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首先什么 ...

  9. U3d 手游优化概述

    移动平台瓶颈 体积小 芯片要求改 功耗小 影响计算系能 带宽小 传输方面受限 性能优化 资源方面 美术方面 自带地形(地形是非常占用资源的) a.控制地形的分辨率 b.地形高度图尺寸小于257 c.地 ...

  10. ReportViewer动态加载数据源

    ReportViewer主要用于打印和导出数据到pdf或excel,接下来将简单做一张Northwind的Products表的统计报表. (最终图) 一.新建一张报表 二.添加数据集 添加xsd文件后 ...