【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 ...
随机推荐
- RMQ with Shifts(线段树)
RMQ with Shifts Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Pra ...
- Delphi的未来,一点浅见
我是新手评议谈不上,但个人认为必须得跟主流大佬走,这同时也得有自己的核心技术,才最终能让自己成为大佬. ------------------------------------------------ ...
- android 半透明弹窗
<style name="edit_AlertDialog_style" parent="@android:style/Theme.Dialog"> ...
- kettle连接资源库设置
到这里你是登陆不上去的,需要创建或更新按钮,因为需要在你的数据库里创建关于kettle的数据表,来存储资源库 点执行就可以了 一般情况下kettle资源库自动给你创建两个用户: 工具->资源库- ...
- 关于树莓派Pi2通过UART连接攀藤G5传感器的python
1.准备工作:树莓派Pi2板子,攀藤G5传感器 关于树莓派40pin口网上很多,我们只了解与攀藤G5连接的问题 (1)攀藤G5pin1(VCC5v)要注意是5V,有很多板子接的是3V,而树莓派的pin ...
- LeetCode:最长回文子串【5】
LeetCode:最长回文子串[5] 题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: ...
- rest-client restclient get post写法
get url = "https://api.weixin.qq.com/sns/jscode2session" data = { appid: "××××", ...
- 高性能javascript学习总结(1)--加载与运行
一.脚本的位置 我们知道,一个<script>标签可以放在 HTML 文档的<head>或<body>标签中,但是浏览器是怎么加载和执行这些java ...
- OSI 与 TCP/IP
OSI参考模型 物理层(Physical Layer) --- 数据表示.物理层规定了激活.维持.关闭通信端点之间的机械特性.电气特性. 功能特性以及过程特性.该层为上层协 ...
- springboot5
1.改造购物车系统 1.1.创建购物车的Spring Boot工程 1.1.导入依赖 <project xmlns="http://maven.apache.org/POM/4.0.0 ...