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, ...
随机推荐
- 数据库还原总提示空间不够,磁盘卷 'D:\' 上的可用空间不足,无法创建数据库
从数据库上备份下来bak格式的数据库文件之后,在本地数据库欢迎的时候总是提示空间不够. 这种情况一般在从64位电脑上面备份的数据库文件,还原到32位的sqlsever上面. System.Data.S ...
- bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token
========4 关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...
- Eclipse下快速打开本地文件插件EasyExplorer(转)
EasyExplorer 是一个类似于 Windows Explorer的Eclipse插件,它可以帮助你在不退出Eclipse的环境下浏览本地文件系统,类似的插件也有很多,但是本人喜欢使用这个版本 ...
- 使用Visual Studio 2013 调试 MASM 汇编程序!
原文地址:http://kipirvine.com/asm/debug/vstudio2013/index.htm Using the Microsoft Visual Studio 2013 Deb ...
- Python中用datetime包进行对时间的一些操作
1. 计算给出两个时间之间的时间差 import datetime as dt # current time cur_time = dt.datetime.today() # one day pre_ ...
- Python中在脚本中引用其他文件函数的方法
在导入文件的时候,Python只搜索当前脚本所在的目录,加载(entry-point)入口脚本运行目录和sys.path中包含的路径例如包的安装地址.所以如果要在当前脚本引用其他文件,除了将文件放在和 ...
- mysql 增加用户
mysql 增加用户 3.增加用户: (注意:和上面不同,下面的因为是MYSQL环境中的命令,所以后面都带一个分号作为命令结束符) 格式:grant select on 数据库.* to 用户名@登录 ...
- C++学习36 string类和字符串
C++大大增强了对字符串的支持,除了可以使用C风格的字符串,还可以使用内置的数据类型 string.string 类处理起字符串来会方便很多,完全可以代替C语言中的 char 数组或 char 指针. ...
- 怎么在logcat中显示system.com.print中的打印信息
在logcat中显示信息可以用Log.v() Log.d() Log.i() Log.w() 以及 Log.e() 1.Log.v 的调试颜色为黑色的,任何消息都会输出: 2.Log.d的输出颜色是蓝 ...
- Hadoop有关的网站
软件下载: http://archive.apache.org hbase对Hadoop的支持矩阵: https://hbase.apache.org/book.html#configuration