【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

计数规则:

  1. 相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3
  2. 小的数字在大的数字右边,所表示的数等于这些数字相加得到的数,例如:VIII = 8
  3. 小的数字,限于(I、X和C)在大的数字左边,所表示的数等于大数减去小数所得的数,例如:IV = 4
  4. 正常使用时,连续的数字重复不得超过三次
  5. 在一个数的上面画横线,表示这个数扩大1000倍(本题只考虑3999以内的数,所以用不到这条规则)

从前向后遍历罗马数字,如果某个数比前一个数小,则加上该数。反之,减去前一个数的两倍然后加上该数

 1 class Solution {
 2 public:
 3     int romanToInt(string s) {
 4         map<char,int> hash;
 5         hash[‘I‘] = 1;
 6         hash[‘V‘] = 5;
 7         hash[‘X‘] = 10;
 8         hash[‘L‘] = 50;
 9         hash[‘C‘] = 100;
10         hash[‘D‘] = 500;
11         hash[‘M‘] = 1000;
12
13         int result=hash[s[0]];
14         for(int i=1;i<s.length();i++)
15         {
16             int pre=hash[s[i-1]];
17             int cur=hash[s[i]];
18             if(pre>=cur)
19             {
20                 result+=cur;
21             }
22             else
23             {
24                 result+=cur-2*pre;
25             }
26
27         }
28         return result;
29     }
30 };
时间: 2024-08-06 07:58:44

【leetcode】Roman to Integer的相关文章

【leetcode】Roman to Integer &amp; Integer to Roman

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 首先,学习一下罗马数字,参考罗马数字 罗马数字是最古老的数字表示方式,比阿拉伯数组早2000多年,起源于罗马 罗马数字有如下符号: 基本字符 I V X L C D M 对应阿拉伯数字 1 5 10 50 100 500 1000 计数规则: 相同的数字连写,所表示的数等于这些数字相

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

【LeetCode】String to Integer (atoi) 解题报告

这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the pos

【LeetCode】String to Integer (atoi) 解题报告 (Java)

这道题在LeetCode OJ上难道属于Easy,但是通过率却比较低,究其原因是需要考虑的情况比较低,很少有人一遍过吧. [题目] Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the poss

【leetcode】String to Integer (atoi)

String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended f

【leetcode】7. Reverse Integer

题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 这道题比较简单,只要注意两个问题:1,输入可能有123,-123两种情况.2,可能会出现值溢出的情况,所以先用long类型处理,决定没有溢出后再转换为int 具体代码: 1 public static int reverse(int x) { 2 String s =""+x; 3

【Leetcode】 #7 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 测试用例: 123………………321 -123………………-321 1200………………21 45134543545…………0(overflow) 2147483646………………0(虽然它小于最大值2147483647,但是调转过来之后溢出!) 最后两个陷阱是不容易发现的. int类型的最大值是2147483647

【Leetcode-easy】Roman to Integer

罗马数字转化为整数 * 1.基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: * 2.不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: * 3.V 和 X 左边的小数字只能用 Ⅰ: * 4.L 和 C 左边的小数字只能用X: * 5.D 和 M 左边的小数字只能用 C. 思路:顺序读取字符,如果前面后面一位大于前面一位,则

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,