Given a roman numeral, convert it to an integer.

The answer is guaranteed to be within the range from 1 to 3999.

Have you met this question in a real interview?

Yes
Example

IV -> 4

XII -> 12

XXI -> 21

XCIX -> 99

题意

计数方法:

基本字符
I
V
X
L
C
D
M
相应的阿拉伯数字表示为
1
5
10
50
100
500
1000
  1. 相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;
  2. 小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12;
  3. 小的数字(限于 I、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;
  4. 正常使用时、连写的数字重复不得超过三次;
  5. 在一个数的上面画一条横线、表示这个数扩大 1000 倍。

组数规则:

有两条须注意掌握:
  1. 基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
  2. 不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个;

解法一:

 class Solution {
public:
/*
* @param s: Roman representation
* @return: an integer
*/
int romanToInt(string s) {
int ans = ;
ans = toInt(s[]); for (int i = ; i < s.length(); i++) {
ans += toInt(s[i]); if (toInt(s[i-]) < toInt(s[i])) {
ans -= toInt(s[i-]) * ;
}
} return ans;
} int toInt(char s) {
switch(s) {
case 'I':return ;
case 'V':return ;
case 'X':return ;
case 'L':return ;
case 'C':return ;
case 'D':return ;
case 'M':return ;
}
return ;
}
};
 

419. Roman to Integer【medium】的更多相关文章

  1. 13. Roman to Integer【leetcode】

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

  2. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  3. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  4. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  5. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  6. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  7. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  8. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  9. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

随机推荐

  1. 类似于GROUP BY SUM() 用于字符串连接的语句

    CREATE TABLE T ( [f1] VarCHAR(100), [f2] VarCHAR(100))goINSERT INTO T   VALUES ('a','abc')INSERT INT ...

  2. Java程序猿笔试面试之String4

    怎样删除String中反复的字符good? 思想一:蛮力法,进行双重循环,此算法的复杂度为O(n^2),n是指字符串的长度 public class RemoveSameChar { public s ...

  3. OpenCV学习(8) 分水岭算法(2)

        现在我们看看OpenCV中如何使用分水岭算法.     首先我们打开一副图像:    // 打开另一幅图像   cv::Mat    image= cv::imread("../to ...

  4. ZeroMQZeroMQ研究与应用分析

    1  ZeroMQ概述 ZeroMQ是一种基于消息队列的多线程网络库,其对套接字类型.连接处理.帧.甚至路由的底层细节进行抽象,提供跨越多种传输协议的套接字.ZeroMQ是网络通信中新的一层,介于应用 ...

  5. 直播 背景 技术体系 乐视云直播Demo

    背景 最近工作需要做一款直播APP,恩是的,从RTMP协议的实现开始到处理服务器高并发.负载均衡.客户端播放器实现等等等..... 估计全部写完我也到而立之年了吧...... BOSS们估计也是发现了 ...

  6. C++标准库 bitset

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-bitset.html,转载请注明源地址. 有些程序要处理二进制位的有序集,每个位可能包含 0(关)1(开)值 ...

  7. CSDN日报20170404 ——《不不过写代码,而是完毕作品》

    [程序人生]不不过写代码,而是完毕作品 作者:瞬息之间 近来有人问起,如今似乎真得变成了码农,日出而作,日落而息.整天不停的写代码,开发业务需求,周而复始,日子长了,感到厌倦. 有时回忆,应该在过去的 ...

  8. STL - 迭代器 - 安插型迭代器

    list<, , , , , , , , }; cout << "** collection 1: **" << endl; ContainerUti ...

  9. 从主机系统向虚拟机系统里面copy 文件

    从主机系统向虚拟机系统里面copy 文件: 一:请确保你的虚拟机里面安装了 VMTools 1:安装VMTools 2: 进入虚拟机系统里面,如果没有自动运行 VMtool安装程序,请打开我的电脑,手 ...

  10. 算法笔记_144:有向图强连通分量的Tarjan算法(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...