13. Roman to Integer
Easy

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
 class Solution {
public:
int romanToInt(string s) {
int ans = ;
int top = ;
int len = s.length();
while(top<len){
if(s[top]=='M'){
ans += ;
top++;
}
else if(s[top]=='D'){
ans += ;
top++;
}
else if(s[top]=='C'){
if(s[top+]=='D'){
ans += ;
top+=;
}
else if(s[top+]=='M'){
ans +=;
top+=;
}
else {
ans += ;
top++;
}
}
else if(s[top]=='L'){
ans+=;
top++;
}
else if(s[top]=='X'){
if(s[top+]=='C'){
ans += ;
top+=;
}
else if(s[top+]=='L'){
ans += ;
top+=;
}
else {
ans += ;
top++;
}
}
else if(s[top]=='V'){
ans+=;
top++;
}
else if(s[top]=='I'){
if(s[top+]=='X'){
ans += ;
top+=;
}
else if(s[top+]=='V'){
ans += ;
top+=;
}
else {
ans += ;
top++;
}
}
}
return ans;
}
};

Leetcode 13. Roman to Integer(水)的更多相关文章

  1. Leetcode#13. Roman to Integer(罗马数字转整数)

    题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...

  2. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  3. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

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

  4. Java [leetcode 13] Roman to Integer

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

  5. LeetCode 13. Roman to Integer(c语言版)

    题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...

  6. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  7. [leetcode]13. Roman to Integer罗马数字转整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  8. [LeetCode] 13. Roman to Integer ☆☆

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

  9. [leetcode] 13. Roman to Integer

    如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...

随机推荐

  1. Linux操作系统目录一览表

    / // 根目录 /bin //存放必要的命令 比如ls.cp.mkdir等命令 /boot //存放内核以及启动所需的文件 /dev //存放硬件设备文件 比如声卡.磁盘.光驱 /etc //存放系 ...

  2. Scrapy输出文件格式问题汇总

    Q:Scrapy抓取的内容(包含中文)输出到JSON Lines文件时如何确保输出的是字符本身而不是其unicode编码? A:默认的JsonLinesItemExporter其ensure_asci ...

  3. Maven 项目中的groupId和artifactId

    maven进行项目管理,如果我们要将项目加入到maven到本地仓库中,则需要对项目进行唯一性标示,而groupId和artifactId就起到这样对作用. groupId为项目组织对唯一标识符,可以理 ...

  4. 字符串中的replace方法

    String.prototype.replace() 该方法作为字符串中非常常用的方法, 今天我们具体介绍一下它的用法 语法格式 someString.replace(regxp | substr, ...

  5. c++多线程并发学习笔记(1)

    共享数据带来的问题:条件竞争 避免恶性条件竞争的方法: 1. 对数据结构采用某种保护机制,确保只有进行修改的线程才能看到修改时的中间状态.从其他访问线程的角度来看,修改不是已经完成了,就是还没开始. ...

  6. pathlib模块替代os.path

    pathlib模块替代os.path 在Python 3.4之前和路径相关操作函数都放在os模块里面,尤其是os.path这个子模块,可以说os.path模块非常常用.而在Python 3.4,标准库 ...

  7. docker容器配置hosts

    在mac开发的时候,docker容器没有配置hosts,但是mac本机配置了hosts,这个本机的hosts配置对docker容器里面的所有容器都适用,但是到了linux的时候反而不适用了 可以通过下 ...

  8. solr集群搭建(SolrCloud)

    SolrCloud(solr 云)是 Solr 提供的分布式搜索方案,当你需要大规模,容错,索引量很大,搜索请求并发很高时可以使用SolrCloud.它是基于 Solr 和Zookeeper的分布式搜 ...

  9. Paper Reading_Distributed System

    最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 Mark一个上海交通大学东岳网络工作室的paper notebook Mark一个大神的笔记 Ed ...

  10. python3中编码与解码的问题

    python3中编码与解码的问题 ASCII .Unicode.UTF-8 ASCII 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此 ...