13. Roman to Integer[E]罗马数字转整数
题目
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),第一个元素作为键值key,map不允许有相同的键值,第二个值是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]罗马数字转整数的更多相关文章
- 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
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- 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(罗马数字转为整数)
题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description int toNumber(char ch) { switc ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- 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 ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
随机推荐
- Dependency Injection in ASP.NET MVC
原文引自http://www.dotnetcurry.com/ShowArticle.aspx?ID=786 1.传统三层结构,相邻两层之间交互: 2.如果使用EntityFramework则View ...
- Java为什么需要基本类型
提问 首先抛出问题吧:Java为什么需要基本类型? 最开始遇到这个问题的场景不记得了,但是一查之下,发现一个问题:Java既然是面向对象的,宣称一切都是对象,为什么还有基础类型. 先上结论 为了性能+ ...
- Jmeter报内存溢出解决方案
描述:wimdows环境,做上传图片接口测试,涉及图片合成和上传,图片采用base64编码.每1s启动200线程的时候,Jmeter报内存溢出错误. 解决方案: 1.修改jmeter.bat: set ...
- oc懒加载 & swift lazy
oc的懒加载依赖于属性的双重属性的函数属性部分. 懒加载的本质是执行get函数. swift的lazy,理论上与此类似. 编译器优化时可能对初始化块进行了保存. 懒加载的本质是延迟执行. 只要是执行, ...
- 优动漫PAINT新建文件
在优动漫PAINT软件中展开任何一项操作之前,都需要新建或打开图形文件.新建文件之后,用户可根据自己的需求进行相应的设置,这样将大大节省后期制作的时间! 在优动漫PAINT中新建图形文件的方法: 方法 ...
- USACO 2008 Mar Silver 3.River Crossing 动态规划水题
Code: #include<cstring> #include<algorithm> #include<cstdio> using namespace std; ...
- 解决phpstudy mysql 启动不了的问题
1.端口监测 查看3306 的端口是否被占用,如占用,停止进程 2.服务没有启动.因为学习python 我把phpstudy的mysql升级到了mysql8.0. sc delete mysql 删 ...
- kali 安装nessus
下载home版: http://www.tenable.com/products/nessus/select-your-operating-system#tos 获取激活码:http://www.te ...
- js中浏览器兼容startsWith 、endsWith 函数
在做js开发的时候用到了startsWith函数时,发现各个浏览器不兼容问题,因为对开发来说,chrome浏览器最好用,就一直在chrome浏览器中使用这两个函数没有任何问题,但在ie浏览器访问就直接 ...
- hdu 4826
hdu 4826 题意 度度熊是一只喜欢探险的熊,一次偶然落进了一个 $ m * n $ 矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格, ...