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 ...
随机推荐
- C-C语言概述
1.数据+算法=程序. 2.C语言程序是由一个或多个函数组成的,函数是由语句组成的,语句是由关键字,标识符,运算符,数据组成的:语句可分为:声明语句,赋值语句,控制语句,函数语句,空语句. 3.#in ...
- video相关简单的api
video 关键api 1. video.pause() 2. video.play() 3. video.webkitRequestFullScreen() //全屏 4. video.curren ...
- mybatis 高级映射和spring整合之高级映射(4)
mybatis 高级映射和spring整合之高级映射 ----------------学习结构-------------------- 0.0 对订单商品数据模型进行分析 1.0 高级映射 1.1 一 ...
- WEB笔记-1、HTML 标记与文档结构
1.HTML 标记与文档结构 1.1 块级(block)和行内(inline)标签 块级标签 <h1>-<h6> : 6级标签,h1表示最重要(h1 不仅仅是最大最突出 ...
- Kinect安装与配置(openNI2)
原文链接:http://blog.csdn.net/chenxin_130/article/details/8580636 简介 最近OpenNI2的推出,小斤也要多给博客除除草了,并在闲暇之余做一些 ...
- CSS读书笔记(1)---选择器和两列布局
(1)CSS选择器优先权选择. 优先权从大到小的选择如下: 标有!important关键字声明的属性 HTML中的CSS样式属性 <div style="color:red" ...
- 00--C++牛人的博客
那些C++牛人的博客 这篇文章来自转载,转载的网址找不到了, 如果有人知道,可以在下面评论, 非常感谢,更感谢原作者. 现整理收集C++世界里那些“牛人”的个人博客.凡三类:一是令人高山仰止的 ...
- (转) RabbitMQ学习之工作队列(java)
http://blog.csdn.net/zhu_tianwei/article/details/40887717 参考:http://blog.csdn.NET/lmj623565791/artic ...
- spring4+hibernate4+struts2环境搭建
tomact配置请查看下面的文章 javaEE_maven_struts2_tomcat_first http://www.cnblogs.com/luotuoke/p/4543686.html po ...
- Python基础:编码
1:先说python2py2里默认编码是ascii文件开头那个编码声明是告诉解释这个代码的程序 以什么编码格式 把这段代码读入到内存,因为到了内存里,这段代码其实是以bytes二进制格式存的,不过即使 ...