题目


Given a roman numeral, convert it to an integer.

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

思路


在进行罗马数字转化为整数的过程中,由于罗马数字是按照从左往右从大到小的顺序排列,于是可以分析,当左边罗马数字对应的整数A比右边一个罗马数字对应的整数B小的时候,表示B-A。

利用map建立一个罗马数字与整数的对应映射,检查字符串当前字符与下一个字符对应整数的大小关系。

这种建立查找表的题可以通过map来解决。

Tips


Map(C++)

1. 定义

所有元素都是pair,同时拥有键值(key)和实值(value),第一个元素作为键值keymap不允许有相同的键值,第二个值是key对应的value值。key和value可以是任意需要的类型。

2. 构造函数

map<int, string> newMap;

3. 添加数据

map<int, string> newMap;
pair<int , string> add(1,"a");
newMap.insert(add);

for循环(python)

1. 循环定义

for anElement in object:

对象Object是一个集合,可以遍历每一个元素

2. 利用range生成整数序列

  • 三个参数:起始值、终值、步长

    • 起始值如果不提供则默认为0
    • 终值是必须的参数,如果只有一个参数,那么该参数就是终值
    • 步长默认为1,只有提供三个参数时,才有步长值
range(5)
[0, 1, 2, 3, 4]
range(3,8)
[3, 4, 5, 6, 7]
range(1,20,4)
[1, 5, 9, 13, 17]

3. 不能改变变量值

在python中,for循环相当于一个迭代器,在循环体改变循环变量的值对循环次数是没有影响的。因此,对于需要改变变量值的,可以改用while循环。

词典结构(python)

利用查找键值来查找对应的实值

C++

class Solution {
public:
int romanToInt(string s) {
map<char, int> table={{'I', 1}, {'V', 5}, {'X',10}, {'L', 50}, {'C',100}, {'D', 500}, {'M', 1000}};
int resVal = 0; for(int i= 0; i < s.length(); i++){
if(i+1 <s.length() && table[s.at(i)] < table[s.at(i+1)]){
resVal += table[s.at(i+1)] - table[s.at(i)];
i++;
continue;
}
resVal += table[s.at(i)];
}
return resVal;
}
};

Python

 def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
table = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
resInt = 0
i = 0
while i < len(s):
if i > 0 and table[s[i]] > table[s[i - 1]]:
resInt += table[s[i]] - 2 * table[s[i - 1]]
else:
resInt += table[s[i]]
i += 1
return resInt

13. Roman to Integer[E]罗马数字转整数的更多相关文章

  1. 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 ,即 ...

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

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

  3. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  4. LeetCode 13 Roman to Integer(罗马数字转为整数)

    题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description   int toNumber(char ch) { switc ...

  5. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

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

  7. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. 13. Roman to Integer【leetcode】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

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

随机推荐

  1. Linux下实现视频读取

    V4L(video4linux是一些视频系统,视频软件.音频软件的基础,经常时候在需要采集图像的场合,如视频监控,webcam,可视电话,经常使用在embedded linux中是linux嵌入式开发 ...

  2. 模拟试题B

    模拟试题B 一.单项选择题(2′*8 =16′) 1.灰度等级为256级,分辨率为2048*1024的显示器,至少需要的帧缓存容量为( ) A)512KB B)1MB C)2MB D)3MB 2.在多 ...

  3. IOS Html富文本渲染方式:DTCoreText、WKWebView、UIWebView的内存占用对比

    在app的内容页(详情页)中,富文本的显示一直是经常需要处理的问题,而通常在后端的富文本编辑中,Html应用比较普遍,所以其实需要处理的Html富文本显示的问题,以下这三种方式肯定不是最优的显示Htm ...

  4. ZBrush关于遮罩的一些操作

    本文讨论使用ZBrush®软件如何在屏幕上创建遮罩和操纵遮罩. 1. 绘制遮罩 按下Ctrl键你就能够在你的模型上绘制遮罩(笔刷的笔划的开始和结束都必须在模型上),默认情况下,遮罩区域在模型上显示为一 ...

  5. 创建一个dynamics CRM workflow (三) - Creating Configuration Entity for Custom Workflow

    上个帖子中, 我们创建了个发email的workflow. 但是我们邮件当中的tax 值是 hard code, 这在开发当中是不容许的. 那今天我们来把这个build in workflow用 in ...

  6. day28 re(正则)模块

    目录 re模块有什么用? re模块的基础使用 元字符 终极 贪婪模式 非贪婪模式 re模块高级 comple match和search re.split() sub和subn 分组 re模块有什么用? ...

  7. C语言提高 (2) 第二天 用指针对字符串进行操作

    2 昨日回顾 p++: (把地址转换成整型 加上它所指向的数据的大小 3指针成立条件和间接赋值 条件一:有两个变量 其中至少一个是指针 条件二:建立关联 条件三:间接操作 4间接操作的例子 5间接操作 ...

  8. 配置mysql允许远程访问

    1.进入 mysql: /usr/local/mysql/bin/mysql -u root -p 2.使用 mysql库 : use mysql; 3.查看用户表 : SELECT `Host`,` ...

  9. 3.2、Ansible单命令测试

    0.Ansible的group支持all.通配符(*).IP地址 1.查看Ansible的版本 $  ansbile  --version [root@test ~]# ansible --versi ...

  10. cmake处理多源文件目录

    cmake处理多源文件目录 假设我们的源文件全部在src中,则我们需要在子文件src中建立文件CmakeLists.txt,内容如下: AUX_SOURCE_DIRECTORY(. DIR_TEST_ ...