leetcode 13

- 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
- 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
- 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9;
- 在一个数的上面画一条横线,表示这个数增值 1,000 倍,如
=5000。
思路:将罗马数字的每个字母按照顺序转换成整数存入数组中;然后按照上述规则操作数组,得到最后的结果。
代码如下:
class Solution {
public:
int romanToInt(string s) {
int result = ;
int last = ;
vector<int> tag;
int n = s.length();
for(int i = ; i < n; ++i)
{
switch (s[i])
{
case 'I':
tag.push_back();
break;
case 'X':
tag.push_back();
break;
case 'C':
tag.push_back();
break;
case 'M':
tag.push_back();
break;
case 'V':
tag.push_back();
break;
case 'L':
tag.push_back();
break;
case 'D':
tag.push_back();
break;
}
result = result + tag[i];
}
for(int i = ; i < n-; ++i)
{
if(tag[i] == || tag[i] == || tag[i] == )
{
if(tag[i] < tag[i+])
{
result = result - * tag[i];
}
}
}
return result;
}
};
leetcode 13的更多相关文章
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- LeetCode 13. 罗马数字转整数(Roman to Integer)
13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符 数值 I 1 V ...
- Java实现 LeetCode 13 罗马数字转整数
13. 罗马数字转整数 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 ...
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
- 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
一.题目链接:https://leetcode.com/problems/roman-to-integer/ 二.题目大意: 给定一个罗马数字,返回它的整数形式. 三.题解: 这道题与12题恰好相反, ...
- LeetCode 13 Roman to Integer(罗马数字转为整数)
题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description int toNumber(char ch) { switc ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
随机推荐
- jQuery部分源码帮助理解
(function(window){})(window) 为什么要传window给jquery当参数呢? 1.为了压缩有 引用 2.加速变量的寻找,当找window对象的时候,默认从本级开始寻找,一级 ...
- StateMachine
Create State Machine Create either a passive or an active state machine: ? 1 var fsm = new PassiveSt ...
- OC字符串常用函数
创建一个字符串对象: NSstring * str1 = @"hello"; NSString * str = [[NSString alloc]initWithString:@& ...
- 慎用StringEscapeUtils.escapeHtml方法【转】
推荐使用Apache commons-lang的StringUtils来增强Java字符串处理功能,也一直在项目中大量使用StringUtils和StringEscapeUtils这两个实用类. 最近 ...
- arm-linux-objdump
一.arm-linux-objdump常用来显示二进制文件信息,常用来查看反汇编代码二.常用选项:1.-b bfdname 指定目标码格式2.—disassemble或者-d 反汇编可执行段3.—di ...
- [ActionScript 3.0] AS3 判断字符串是否为数字
trace(isNaN(Number("0")));//false trace(isNaN(Number("123")));//false trace(isNa ...
- 在delphi下TClientSocket的使用技巧 转
http://blog.csdn.net/newzhhsh/article/details/2905874 如果你是在线程的构造函数中创建TClientSocket,那么TClientSocket还是 ...
- centos 下安装.net core
先要安装libunwind, libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,32位操作系统不要安装.其中包括用于输出堆栈跟踪的API.用于以编程方式辗转开解堆栈的 ...
- 安装Ubuntu下的开发工具
刚安装好的Ubuntu,还缺乏很多开发工具.这些工具都可以通过网络进行安装. 1. 更新软件源$ sudo apt-get update 2.安装.配置.启动ftp服务.执行以下命令安装,安装后即会自 ...
- OC基础(23)
NSArray基本概念 NSArray 遍历 NSArray排序 NSArray文件读写 NSArray 与字符串 *:first-child { margin-top: 0 !important; ...