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. V-rep学习笔记:转动关节1

    V-REP(Virtual Robot Experimentation Platform),是全球领先的机器人及模拟自动化软件平台.V-REP让使用者可以模拟整个机器人系统或其子系统(如感测器或机械结 ...

  2. Using Pre-Form Trigger In Oracle Forms

    Pre-Form trigger in Oracle Forms fires during the form start-up, before forms navigates to the first ...

  3. C#:常规属性和自动实现的属性

    根据属性的实现方式,属性可分为自动实现的属性和常规属性. 常规属性需要具体的人为的实现get访问器或者set访问器,而且一般需要有一个字段与之相对应:而自动实现的属性的get和set访问器的实现部分被 ...

  4. 【CC评网】2013.第44周 把握每天的第一个小时

    [CC评网]2013.第44周 把握每天的第一个小时 更简单的格式 终于投入到markdown的怀抱.让博客的写作回归到内容本身,同时也能保证阅读的良好体验:如果有心情,写个js,提取h3 h2标题组 ...

  5. 中国用户mac上快速安装nodejs

    mac nodejs 安装 1.http://npm.taobao.org/mirrors/node/latest/ 进入这个域名,然后找到最新的pkg包下载过来 2.双击pkg包,下一步下一步安装 ...

  6. BeanUtils框架的简单运用

    Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单.易用的API操作Bean的属性——BeanUtils Beanutils工具包的常用类: •BeanUt ...

  7. Ubuntu 通过Deb安装 MySQL5.5(转载)

    1. 下载 MySQL 5.5 deb 安装包 cd /usr/local/src sudo wget -O mysql-5.5.22-debian6.0-i686.deb http://dev.my ...

  8. Nginx基础知识之——配置文件信息(检查配置文件是否正确)

    一.检查配置文件是否正确: /usr/local/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 检查结果: nginx: [emerg] ngin ...

  9. ccc

    课本第291页第4题 #include<stdio.h> void main() { int n, m, i, k; int p_begin; ]; scanf("%d" ...

  10. asp.net实现大文件上传

    需要下载NeatUpload插件 上传页面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile ...