【LeetCode】013. Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题解:
只要知道罗马数字的规律即可
|
基本字符
|
I
|
V
|
X
|
L
|
C
|
D
|
M
|
|
相应的阿拉伯数字表示为
|
1
|
5
|
10
|
50
|
100
|
500
|
1000
|
class Solution {
public:
int romanToInt(string s) {
int n = s.size();
int res = ;
unordered_map<char, int> map = { { 'I' , },
{ 'V' , },
{ 'X' , },
{ 'L' , },
{ 'C' , },
{ 'D' , },
{ 'M' , } };
for (int i = ; i < n; ++i) {
int num = map[s[i]];
if (i == n - || map[s[i + ]] <= map[s[i]])
res += num;
else
res -= num;
}
return res;
}
};
【LeetCode】013. Roman to Integer的更多相关文章
- 【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 ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【leetcode】13. Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...
- 【一天一道LeetCode】#13. Roman to Integer
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- ios开发:如何加载大量图片 相册示例
本文转载至 http://www.cnblogs.com/xiongqiangcs/archive/2013/06/13/3134486.html 1. Create a NSOperationQ ...
- maven;tomcat配置
[说明]今天呀,上午刚刚打算写javaweb项目,服务器就出现了问题,就花了点时间搞定了:下午听老大说了任务的大致内容和意义,收获颇多:晚上去服务器上部署了maven,听说可以实现热部署 一:今天完成 ...
- nginx学习之反向代理篇(六)
在本节,你将学会: --如何根据不同的协议,将请求转发到后端服务器: --修改发送到后端服务器的请求头: --以及配置是否buffering从后端服务器返回来的响应. 1. 将请求转发给后端服务器 当 ...
- jauery table
$("#tableData tr:gt(0)").each(function() { }//橘色部分是查找id为tableData的DataTable里面除第一行以外的行
- bug_1——oracle listagg():列转行
select listagg(字段名 ,',') within group (order by 字段名) from表 where 条件 listagg():列转行 WM_CONCAT():和并列 ...
- linux基础part2
linux基础 一.linux基础命令 1.pwd:用来显示当前目录位置 2.cd:用来切换目录位置.(eg:cd...cd../...cd-.cd~) 3.ls:用来查看目录或文件信息(eg:ls ...
- 通过套接字(socket)和UDP协议实现网络通信
UDP---用户数据报协议,是一个简单的面向数据报的运输层协议.(无连接.封包.大小限制.速度快). 一.UDP协议的特点: 将数据及源和目的地封装成数据包中,不需要建立连接. 每个数据报的大小限制在 ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 0428 正则表达式 re模块
复习 异常处理try except 一定要在except之后写一些提示或者处理的内容 try: '''可能会出现异常的代码'''except ValueError: '''打印一些提示或者处理的内容' ...
- jQuery宽屏游戏焦点图
在线演示 本地下载