Roman to Integer [LeetCode]
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]的更多相关文章
- 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 ...
- Roman To Integer leetcode java
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- 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 ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- 【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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- mac 配置jdk maven
1.从oracle下载jdk 链接:http://www.oracle.com/technetwork/java/javase/downloads/index.html 然后安装jdk 2.下载Mav ...
- How can I retrieve the remote git address of a repo?
When you want to show an URL of remote branches, try: git remote -v
- hdu 1116 Play on Words 欧拉路径+并查集
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟
C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 详解C#委托,事件与回调函数
.Net编程中最经常用的元素,事件必然是其中之一.无论在ASP.NET还是WINFrom开发中,窗体加载(Load),绘制(Paint),初始化(Init)等等.“protected void Pag ...
- php 上传文件。$_FILES
<form name="article" method="post" enctype="multipart/form-data" ac ...
- iOS - Swift NSRect 位置和尺寸
前言 结构体,这个结构体用来表示事物的坐标点和宽高度. public typealias NSRect = CGRect public struct CGRect { public var origi ...
- HDU5869树状数组+gcd预处理
比赛的时候知道用树状数组,但有点乱不知道怎么处理. 统计不同的gcd的个数其实就是用树状数组统计区间内不同的数的模板题啊... 复杂度O(nlogn) #include <bits/stdc++ ...
- 给input的按钮控件添加onserverclick事件
前台: <input type="button" value="登录" id="login" onclick="" ...
- 关于协程的学习 & 线程栈默认10M
先看的这篇文章:http://blog.csdn.net/qq910894904/article/details/41699541 以nginx为代表的事件驱动的异步server正在横扫天下,那么事件 ...