LeetCode 题解(73): Roman to Integer

题目:

Given a roman numeral, convert it to an integer.

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

题解:

笨办法,switch-case,好办法,hashmap。

C++:

class Solution {
public:
    int romanToInt(string s) {
        int result = 0;
        if(!s.length())
            return result;
        for(int i = 0; i < s.length(); i++) {
            switch(s[i]) {
                case 'M':
                    result += 1000;
                    break;
                case 'D':
                    result += 500;
                    break;
                case 'C':
                    if(i + 1 < s.length()) {
                        switch(s[i+1]) {
                            case 'D':
                                result += 400;
                                i++;
                                break;
                            case 'M':
                                result += 900;
                                i++;
                                break;
                            default:
                                result += 100;
                                break;
                        }
                    } else {
                        result += 100;
                    }
                    break;
                case 'L':
                    result += 50;
                    break;
                case 'X':
                    if(i + 1 < s.length()) {
                        switch(s[i+1]) {
                            case 'L':
                                result += 40;
                                i++;
                                break;
                            case 'C':
                                result += 90;
                                i++;
                                break;
                            default:
                                result += 10;
                                break;
                        }
                    }
                    else {
                        result += 10;
                    }
                    break;
                case 'V':
                    result += 5;
                    break;
                case 'I':
                    if(i + 1 < s.length()) {
                        switch(s[i+1]){
                            case 'V':
                                result += 4;
                                i++;
                                break;
                            case 'X':
                                result += 9;
                                i++;
                                break;
                            default:
                                result += 1;
                                break;
                        }
                    }
                    else {
                        result += 1;
                    }
                    break;
                default:
                    break;
            }
        }
        return result;
    }
};

Java:

public class Solution {
    public int romanToInt(String s) {
        int result = 0;
        if(s.length() == 0)
            return 0;
            
        for(int i = 0; i < s.length(); i++) {
            switch(s.charAt(i)){
                case 'M':
                    result += 1000;
                    break;
                case 'D':
                    result += 500;
                    break;
                case 'C':
                    if(i + 1 < s.length()) {
                        switch(s.charAt(i+1)) {
                            case 'D':
                                result += 400;
                                i++;
                                break;
                            case 'M':
                                result += 900;
                                i++;
                                break;
                            default:
                                result += 100;
                                break;
                        }
                    } else {
                        result += 100;
                    }
                    break;
                case 'L':
                    result += 50;
                    break;
                case 'X':
                    if(i + 1 < s.length()) {
                        switch(s.charAt(i+1)) {
                            case 'L':
                                result += 40;
                                i++;
                                break;
                            case 'C':
                                result += 90;
                                i++;
                                break;
                            default:
                                result += 10;
                                break;
                        }
                    } else {
                        result += 10;
                    }
                    break;
                case 'V':
                    result += 5;
                    break;
                case 'I':
                    if(i + 1 < s.length()) {
                        switch(s.charAt(i+1)) {
                            case 'V':
                                result += 4;
                                i++;
                                break;
                            case 'X':
                                result += 9;
                                i++;
                                break;
                            default:
                                result += 1;
                                break;
                        }
                    } else {
                        result += 1;
                    }
                    break;
            }
        }
        return result;
    }
}

Python:

class Solution:
    # @return an integer
    def romanToInt(self, s):
        if len(s) == 0:
            return 0
        d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D': 500, 'M': 1000}
        result = 0
        while len(s) > 0:
            if len(s) == 1 or d[s[0]] >= d[s[1]]:
                result += d[s[0]]
                s = s[1:]
            else:
                result = result + d[s[1]] - d[s[0]]
                s = s[2:]
        return result


你可能感兴趣的:(LeetCode,Algorithm,面试题)