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. Cheatsheet: 2013 07.09 ~ 07.20

    Mobile How to implement Android Splash Screen Migrating iOS MVC Applications to Windows Phone 8 (M-V ...

  2. 【转载】前端面试“http全过程”将所有HTTP相关知识抛出来了...

    原文:前端面试“http全过程”将所有HTTP相关知识抛出来了... 来一篇串通,一个http全过程的问题,把所有HTTP相关知识点都带过一遍 http全过程 输入域名(url)-->DNS映射 ...

  3. vsftp软链接ln遇到550错误

    centos 6.3上新建了vsftp ,用于文件下载使用 ./var/ftp/pub可以匿名登陆下载 .如果想要上传则需要使用密码验证 . 登陆使用的账号是ftpclient ,路径在/home/f ...

  4. CUBRID学习笔记 9 创建示例数据库

    如果你安装的时候没有装数据库,可以后期装 其实和上文基本差不多. cd ~ mkdir CUBRID_databases cd CUBRID_databases mkdir demodb cd dem ...

  5. javascript练习----复选框全选,全不选,反选

    第一种方式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  6. iOS - UIRefreshControl 刷新数据

    前言 NS_CLASS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED @interface UIRefreshControl : UIControl 1.UIRefresh ...

  7. iOS - UIGestureRecognizer

    前言 NS_CLASS_AVAILABLE_IOS(3_2) @interface UIGestureRecognizer : NSObject @available(iOS 3.2, *) publ ...

  8. Gradient Boosted Regression Trees 2

    Gradient Boosted Regression Trees 2   Regularization GBRT provide three knobs to control overfitting ...

  9. Oracle 中取当前日期的上个月最后天和第一天

    前一月 最后一天select last_day(add_months(sysdate,-1)) from dual;               2010-2-28 9:23:01; select t ...

  10. SQL server数据类型、增删改查

    数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...