这题也很无聊,连题的内容都不想贴了,就按照规则做吧,挺无聊的

class Solution {
    public int romanToInt(String s) {
        if(s == null || s.length() == 0)
            return 0;

        int res = 0;
        for(int i = 0; i < s.length() - 1; i++) {
            int n1 = getNum(s.charAt(i));
            int n2 = getNum(s.charAt(i + 1));
            if(n1 < n2)
                res -= n1;
            else
                res += n1;
        }

        res += getNum(s.charAt(s.length() - 1));
        return res;
    }

    public int getNum(char c) {
        switch(c) {
            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;
            default:
                return 0;
        }
    }
}

results matching ""

    No results matching ""