LeetCode13 Roman to Integer
题意:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
分析:
开始准备采用将罗马数字的字符串先划分千位,百位等,但是实际操作起来代码太复杂,而且自己想的逻辑也有问题。
最后采用的方式是从前到后遍历一遍字符即可,加判断来查看字符是不是与前面字符一起组成“小数在大数左边的数字”;
然后依次加数到结果进去即可。
代码:
class Solution {
public:
int romanToInt(string s) {
int result = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] == 'M') {
if (i - >= && s[i-] == 'C') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'D') {
if (i - >= && s[i-] == 'C') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'C') {
if (i - >= && s[i-] == 'X') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'L') {
if (i - >= && s[i-] == 'X') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'X') {
if (i - >= && s[i-] == 'I') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'V') {
if (i - >= && s[i-] == 'I') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'I'){
result += ;
}
}
return result;
}
};
LeetCode13 Roman to Integer的更多相关文章
- LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)
1.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range f ...
- LeetCode 13. 罗马数字转整数(Roman to Integer)
13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符 数值 I 1 V ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- 5.Integer to Roman && Roman to Integer
Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
随机推荐
- windows平台下安装python的setuptools工具
到下面的网址下载setuptools-0.6c11.win32-py2.7.exe http://pypi.python.org/pypi/setuptools#files 然后安装setuptool ...
- 【转】jsp页面中jstl标签详解
原文地址: JSLT标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一定要扎实 ...
- POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4438 Acc ...
- LC并联谐振回路
- 用DataBaseMail发图片并茂的邮件
不知道各位的老板有没有这样的要求, 一些系统中的数据需要定时发出邮件提醒, 如呆料就要到期或者一些待办的事项提醒. 当然这些用SSRS报表订阅可以实现,但有些公司没有设定相应的报表服务,又或者只是一些 ...
- uva140 - Bandwidth
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orderi ...
- python中使用list作为默认参数且调用时不给其赋值的问题
最近在写代码时发现一个有趣的地方,当python中的函数使用list作为默认参数且调用时不给其赋值时,无法通过在函数中将其赋值为[]来达到清空此默认参数的目的.按照道理来说,函数f1中的list为局部 ...
- C++ delete operator做了什么事
1.C++中的delete operator做了两件事:调用析构方法和调用operator delete释放内存. 2.考虑析构方法,如果析构方法是虚方法,调用指针真实类型的析构方法,否则调用表面类型 ...
- jquery的匿名函数研究
jQuery片段: ? 1 2 3 ( function (){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...
- C++ Code_ImageList
主题 1. 创建图像列表 2. 使用图像列表绘图 3. 4. 5. 代码::创建图像列表 双击 Cproject03Dlg在 下面添加 1 句 ////////////////// ...