题意:

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的更多相关文章

  1. LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)

    1.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range f ...

  2. LeetCode 13. 罗马数字转整数(Roman to Integer)

    13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符        数值  I           1  V  ...

  3. 【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 ...

  4. 【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 ...

  5. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  6. 5.Integer to Roman && Roman to Integer

    Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...

  7. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  8. 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

    [本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...

  9. No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

随机推荐

  1. JavaScript 链式结构序列化详解

    一.概述 在JavaScript中,链式模式代码,太多太多,如下: if_else: if(...){ //TODO }else if(...){ //TODO }else{ //TODO } swi ...

  2. 借助LVS+Keepalived实现负载均衡(转)

    原文:http://www.cnblogs.com/edisonchou/p/4281978.html 一.负载均衡:必不可少的基础手段 1.1 找更多的牛来拉车吧 当前大多数的互联网系统都使用了服务 ...

  3. MYSQL数据库性能调优之八:mysql日志

    MySQL日志 主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志.中继日志: 使用 SHOW GLOBAL VARIABLES LIKE '%log%';  查询所有日志配置详情: 一. ...

  4. Step By Step(Lua字符串库) (转)

    1. 基础字符串函数:    字符串库中有一些函数非常简单,如:    1). string.len(s) 返回字符串s的长度:    2). string.rep(s,n) 返回字符串s重复n次的结 ...

  5. 深入剖析AutoreleasePool

    [深入剖析AutoreleasePool] Objc的AutoreleasePool是一个首尾相连的内存链接,每块大小为1页(32位机上为4kb). 上面可以看到,parent指向父Pool,chil ...

  6. 关于iTunes11.1 不能刷自制固件的解决方案

    由于iTunes升级到11.1后, 苹果在程序里限制了不允许刷自制固件, 所以想刷自制固件暂时只能降级iTunes到11.1版本之前, 这里提供iTunes 11.0.5 的下载地址: Windows ...

  7. StructLayout特性

    StructLayout特性          公共语言运行库利用 StructLayoutAttribute控制类或结构的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列.如果要将类传递 ...

  8. Firefox常用插件

    一.Web浏览使用插件 1.Adblock Plus广告拦截插件:能够自动拦截很多弹出广告,同时支持右键拦截指定信息 2.惠惠购物助手支持各大购物网站商品实时价格比较,非常棒的网站购物利器,插件下载地 ...

  9. 一分钟制作U盘版BT3 - 有图滴儿 bt3破解教程

    一分钟制作 BT3 U盘版方便,快捷简单无效不退款 光盘版BT3, 大概694MB,直接刻盘,然后用光盘引导,就可以进入bt3,连接为:http://ftp.heanet.ie/mirrors/bac ...

  10. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...