Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Summary: When meeting C/X/I, remembers to search forward to check if there is a bigger number at the front.

     int romanToInt(string s) {
if(s.size() == )
return ; int num = ;
int m_idx = -;
int d_idx = -;
for(int i = ; i < s.size(); i ++) {
if(s[i] == 'C') {
m_idx = s.find_first_of('M', i + );
d_idx = s.find_first_of('D', i + );
if(m_idx > i && m_idx < s.size()){
num += - (m_idx - i)*;
i = m_idx;
}else if(d_idx > i && d_idx < s.size()){
num += - (d_idx - i)*;
i = d_idx;
}else {
num += ;
}
}else if( s[i] == 'M'){
num += ;
}else if(s[i] == 'D') {
num += ;
}else if(s[i] == 'L') {
num += ;
}else if(s[i] == 'X') {
m_idx = s.find_first_of('C', i + );
d_idx = s.find_first_of('L', i + );
if(m_idx > i && m_idx < s.size()){
num += - (m_idx - i)*;
i = m_idx;
}else if(d_idx > i && d_idx < s.size()){
num += - (d_idx - i)*;
i = d_idx;
}else {
num += ;
}
}else if(s[i] == 'V'){
num += ;
}else if(s[i] == 'I') {
m_idx = s.find_first_of('X', i + );
d_idx = s.find_first_of('V', i + );
if(m_idx > i && m_idx < s.size()){
num += - (m_idx - i);
i = m_idx;
}else if(d_idx > i && d_idx < s.size()){
num += - (d_idx - i);
i = d_idx;
}else {
num += ;
}
}
}
return num;
}

Roman to Integer [LeetCode]的更多相关文章

  1. Roman to Integer -- LeetCode 13

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. Roman To Integer leetcode java

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  3. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

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

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

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

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

  7. LeetCode:Roman to Integer,Integer to Roman

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

  8. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

    12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...

  9. 13. Roman to Integer【leetcode】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

随机推荐

  1. MSChart BarChart

    不让合并0-5 chart1.ChartAreas[].AxisX.LabelStyle.Interval = ; 效果

  2. [51NOD1090] 3个数和为0(水题,二分)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1090 找到所有数的和,然后再原数组里二分找符合条件的第三个数. ...

  3. ajax请求、servlet返回json数据

    ajax请求.servlet返回json数据 1.方式一 response.setcontenttype("text/html;charset=utf-8"); response. ...

  4. Timeout expired超时时间已到. 达到了最大池大小 错误及Max Pool Size设置

    此文章非原创,仅为分享.学习!!! 参考数据库链接串: <add key="data" value="server=192.168.1.123; port=3306 ...

  5. STL--list

    List-概述: 列表List是一个线性链表结构(Double—Linked Lists,双链表),它的数据由若干个节点构成,每一个节点都包括一个信息块Info(即实际存储的数据).一个前驱指针Pre ...

  6. namespace使用总结

    1.防止引用文件中函数名相同,导致函数重定义错误: //test1.php <?php namespace foo; function func(){ echo "test1/func ...

  7. SQL HAVING语句

    HAVING 子句 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. SQL HAVING 语法 SELECT column_name, aggregate_f ...

  8. poj1113Wall(凸包)

    链接 顺便整理出来一份自己看着比较顺眼的模板 #include <iostream> #include<cstdio> #include<cstring> #inc ...

  9. access denied ("java.net.SocketPermission" "localhost:1527" "listen,resolve")

    在开启derby服务出现该错误(测试hibernate 连接数据库时  使用myeclipse2014自带的数据库--windows->show view->other->Myecl ...

  10. Android APK混淆

    APK混淆 1 修改project.properties文件 即可实现对项目进行全局混码将proguard.config=${sdk.dir}/tools/proguard/proguard-andr ...