19.1.26 [LeetCode13]Roman to Integer

Roman numerals are represented by seven different symbols: IVXLCD 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.

 1 class Solution {
 2 public:
 3     string sym[13] = { "M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" };
 4     int val[13] = { 1000,900,500,400,100,90,50,40,10,9,5,4,1 };
 5     int findit(string x) {
 6         static int i = 0;
 7         for (i = 0; i < 13; i++)
 8             if (x == sym[i])
 9                 return val[i];
10         return -1;
11     }
12     int romanToInt(string s) {
13         int ans = 0;
14         while (s != "") {
15             if (s.size() >= 2) {
16                 int two = findit(s.substr(0, 2));
17                 if (two != -1) {
18                     ans += two;
19                     s = s.substr(2);
20                     continue;
21                 }
22             }
23             ans += findit(s.substr(0,1));
24             s = s.substr(1);
25         }
26         return ans;
27     }
28 };

原文地址:https://www.cnblogs.com/yalphait/p/10322593.html

时间: 2024-11-08 15:04:10

19.1.26 [LeetCode13]Roman to Integer的相关文章

Leetcode13 Roman to Integer

题意: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 分析: 开始准备采用将罗马数字的字符串先划分千位,百位等,但是实际操作起来代码太复杂,而且自己想的逻辑也有问题. 最后采用的方式是从前到后遍历一遍字符即可,加判断来查看字符是不是与前面字符一起组成“小数在大数左边的数字”: 然后依次加数到结果进去即可. 代码: 1 class

LeetCode13——Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题目大意 给你个罗马数字,把它转换成一个int数.输入限定在[1, 3999]. 难度系数:容易 实现 一次性通过,:) int getVal(char a) { switch (a) { case 'I': return 1; case 'V': return 5; case 'X

LeetCode13 Roman to Integer 罗马转为数字

题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 翻译:把罗马转为数字 思路:如果是单纯的一个罗马字母比较好处理,但是对于4,9这样的,应该看它下一个字符代表的数字是不是比他大,要是大的话减去当前的字符代表的数字.要是小的话,则加上这个数字. 像IV ,代表4.当读到I的时候,应该判断下一个字符V和I的关系,V比I代表的数字

【leetcode】Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 罗马数字有如下符号: 基本字符 I V X L C D M 阿拉伯数字 1 5 10 50 100 500 1000 计数规则: 相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3 小的数字在大的数字右边,所表示的数等于这些数字

58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字,转换为阿拉伯数字.本题只考虑3999以内的数. 罗马数字有如下符号: Ⅰ(1)Ⅴ(5)Ⅹ(10)L(50)C(100)D(500)M(1000) 计数规则: (1).若干相同数字连写表示的数是这些罗马数字的和,如III=3: (2).小数字在大数字前面表示的数是用大数字减去小数字,如IV=4: (3

Integer to Roman &amp; Roman to Integer

Integer to Roman Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range from 1 to 3999. Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals https://zh.wikipedia.org/wiki/%E7%BD%9

Roman to Integer [LeetCode 13]

1- 问题描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 2- 罗马数字[1] 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗马数字中没有“0”,与进位制无关.一般认为罗马数字只用来记数,而不作演算. 重复数次:一个

【leetcode刷题笔记】Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字:否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字.利用一个HashMap存放罗马字符和数字的对应. 罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.

[LeetCode OJ] Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 下面是百度得到的关于罗马数的解释: 我的代码: 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 map<char,int> conversion; 5 conversion.insert(make_pair('