LeetCode 13 Roman to Integer (C,C++,Java,Python)

Problem:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Solution:

时间复杂度O(n)

题目大意:

与12题相反,给一个罗马数字,要求转化为十进制数字

解题思路:

Java源代码(用时749ms):

public class Solution {
    public int romanToInt(String s) {
        int index=0,num=0,temp=0;
        while(index<s.length()){
            char c=s.charAt(index++);
            switch(c){
                case 'I':num+=1;temp=1;break;
                case 'V':num+=temp==1?3:5;break;
                case 'X':num+=temp==1?8:10;temp=10;break;
                case 'L':num+=temp==10?30:50;break;
                case 'C':num+=temp==10?80:100;temp=100;break;
                case 'D':num+=temp==100?300:500;break;
                case 'M':num+=temp==100?800:1000;break;
            }
        }
        return num;
    }
}

C语言源代码(用时18ms):

int romanToInt(char* s) {
    int num=0,temp=0;
    while(*s){
        switch(*s){
            case 'I':num+=1;temp=1;break;
            case 'V':num+=temp==1?3:5;break;
            case 'X':num+=temp==1?8:10;temp=10;break;
            case 'L':num+=temp==10?30:50;break;
            case 'C':num+=temp==10?80:100;temp=100;break;
            case 'D':num+=temp==100?300:500;break;
            case 'M':num+=temp==100?800:1000;break;
        }
        s++;
    }
    return num;
}

C++源代码(用时58ms):

class Solution {
public:
    int romanToInt(string s) {
        int index=0,num=0,temp=0;
        while(index<s.size()){
            char c=s[index++];
            switch(c){
                case 'I':num+=1;temp=1;break;
                case 'V':num+=temp==1?3:5;break;
                case 'X':num+=temp==1?8:10;temp=10;break;
                case 'L':num+=temp==10?30:50;break;
                case 'C':num+=temp==10?80:100;temp=100;break;
                case 'D':num+=temp==100?300:500;break;
                case 'M':num+=temp==100?800:1000;break;
            }
        }
        return num;
    }
};

Python源代码(用时138ms):

class Solution:
    # @param {string} s
    # @return {integer}
    def romanToInt(self, s):
        index=0;num=0;temp=0
        while index<len(s):
            c = s[index];index+=1
            if c=='I':num+=1;temp=1
            elif c=='V':num+=3 if temp==1 else 5
            elif c=='X':num+=8 if temp==1 else 10;temp=10
            elif c=='L':num+=30 if temp==10 else 50
            elif c=='C':num+=80 if temp==10 else 100;temp=100
            elif c=='D':num+=300 if temp==100 else 500
            elif c=='M':num+=800 if temp==100 else 1000
        return num
时间: 2024-10-02 09:25:35

LeetCode 13 Roman to Integer (C,C++,Java,Python)的相关文章

[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 013.Roman_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/roman-to-integer/ 代码(github):https://github.com/illuz/leetcode 题意: 把罗马数转为十进制. 分析: 跟 012. I

Leetcode#13. Roman to Integer(罗马数字转整数)

题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数

leetCode 13. Roman to Integer 字符串

13. 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(1),V(5),X(10),L(50),C(100),D(500),M(1000).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

Leetcode 13. Roman to Integer(水)

13. Roman to Integer Easy 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, two is written as II in Roman numeral, just two one's added together. Twelve i

[LeetCode] 008. String to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 008.String_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/string-to-integer-atoi/ 代码(github):https://github.com/illuz/leetcode 题意: 将一个字符串转化为 int 型.

[LeetCode][13]Roman to Integer解析 罗马字符转int类型关于栈的常数实现-Java实现

Q: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. A: 以下解法和代码没有借阅以往任何资料,如果有更好的解法请在评论区留言 看到这一题我真是感叹城会玩,昨天刚刚解过一个int转罗马字母,今天又反过来解.自叹弗如.这题的该意思就是给一个罗马字符,把他转化成一个int数字,范围在1-3999之间.关于罗马数字的详细信息就不复制了,

LeetCode #13 Roman to Integer #Java

问题 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Tags: Math, String 代码 public class Solution { public int romanToInt(String s) { Map<Character, Integer> map = new HashMap<Character, In

LeetCode 13. Roman to Integer

https://leetcode.com/problems/roman-to-integer/#/description Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 字符串简单题,要搞清楚转换规则.如果当前字母比前一个大,则相减,比如IV = 5 - 1:否则就相加,比如VI = 5 + 1,II = 1 + 1. 罗马数字_

LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Subscribe to see which companies asked this questio 解析:给出一个罗马数字,要求把其转换为一个整数.输入范围在1到3999内. 罗马数字的规则如下: 罗马数字 I V X L C D M 代表的阿拉伯数字 1 5