LeetCode (力扣) 13. Roman to Integer ( C ) - Easy- 暴力解

该博客介绍了如何将罗马数字转换为整数,通过解析输入的罗马数字字符串,根据罗马数字的规则进行加减运算,逐步转换并计算出对应的整数值。文章提供了C语言的解题代码,并展示了代码的时间和空间效率。

同步发于 JuzerTech 网站,里面有我软、硬件学习的纪录与科技产品开箱,欢迎进去观看。

此题为前一题 LeetCode 12. Integer to Roman 的延伸,这次为给定一个罗马数字转换成阿拉伯数字。

题目与范例如下

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, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 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.

Example 1:

Input: s = "III"
Output: 3
Example 2:

Input: s = "IV"
Output: 4
Example 3:

Input: s = "IX"
Output: 9
Example 4:

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

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].

 

解题策略为依序拆解,先从前面的 'M' 转换成千,再转换 'CM' , 'CD' 900 和 400,再转换 'D'以此类推。

 

下方为我的代码

int romanToInt(char * s){
    int num = 0;
    for(int i = 0;i<strlen(s);i++){
        if( s[i] == 'M' ){
            num+=1000;
        }
        
        else if( s[i] == 'C' ){
            if(i+1<strlen(s)){
                if( s[i+1] == 'M' ){
                    num+=900;
                    i++;
                }
                else if( s[i+1] == 'D' ){
                    num+=400;
                    i++;
                }
                else
                    num+=100;
            }
            else{
                num+=100;
            }
        }
        else if( s[i] == 'D' )
            num+=500;
        
        else if( s[i] == 'X' ){
            if(i+1<strlen(s)){
                if( s[i+1] == 'C' ){
                    num+=90;
                    i++;
                }
                else if( s[i+1] == 'L' ){
                    num+=40;
                    i++;
                }
                else
                    num+=10;
            }
            else{
                num+=10;
            }
        }
        else if( s[i] == 'L' )
            num+=50;
        
        else if( s[i] == 'I' ){
            if(i+1<strlen(s)){
                if( s[i+1] == 'X' ){
                    num+=9;
                    i++;
                }
                else if( s[i+1] == 'V' ){
                    num+=4;
                    i++;
                }
                else
                    num+=1;
            }
            else{
                num+=1;
            }
        }
        else if( s[i] == 'V' )
            num+=5;
    }
    return num;
}

 

下方为时间与空间之消耗

Runtime: 4 ms, faster than 84.35 % of C online submissions for Roman to Integer.

Memory Usage: 5.8 MB, less than 58.45 % of C online submissions for Roman to Integer.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值