【leetcode】7 Roman to Integer

罗马字符转整数

注意事项:

1 几个罗马字符对应的整数

‘I‘:  1;
 ‘V‘:  5;
 ‘X‘:  10;
 ‘L‘:   50;
 ‘C‘:  100;
 ‘D‘:  500;
 ‘M‘: 1000;

2 对于DC这种前者大于后者的好处理,

对于CD这种前者小于后者的,相当于C+D-2*C

class Solution {
public:
    int toInt(char x){
        switch(x){
            case ‘I‘: return 1;
            case ‘V‘: return 5;
            case ‘X‘: return 10;
            case ‘L‘: return 50;
            case ‘C‘: return 100;
            case ‘D‘: return 500;
            case ‘M‘: return 1000;
        }
       
    }
    int romanToInt(string s) {
         int rest=toInt(s[0]);
         for(int i=1;i<s.length();i++){
             if(toInt(s[i-1])<toInt(s[i])){
                 rest+=toInt(s[i])-2*toInt(s[i-1]);
             }else
                 rest+=toInt(s[i]);
         }
         return rest;
       
    }
};

时间: 2024-08-10 02:04:35

【leetcode】7 Roman to Integer的相关文章

【LeetCode】6 - Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Solution: 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 个位数举例 Ⅰ-1.Ⅱ-2.Ⅲ-3.Ⅳ-4.Ⅴ-5.Ⅵ-6.Ⅶ-7.Ⅷ-8.Ⅸ-9 十位数举例 Ⅹ-10.Ⅺ-11.Ⅻ-12.XIII-13.XIV-14.

【leetcode】13. Roman to Integer

题目描述: Given a roman numeral, convert it to an integer. 解题思路: 罗马计数法有以下规则: 基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: 不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: V 和 X 左边的小数字只能用 Ⅰ: L 和 C 左边的小数字只能用X: D

【LeetCode】013. 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.相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3: 2.小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,

【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer 题意:完成内置函数atoi的功能,将字符串转换成整数. 教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理.然后按照C++ Reference中关于atoi的规定一条一条写,才AC.另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以

【LeetCode】8. 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 for this problem to be

【LeetCode】8 - 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 for this problem to be spe

【leetcode】1394. Find Lucky Integer in an Array

题目如下: Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value. Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky inte

【LeetCode】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num==0) return sb.toString(); while(num

【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