13. Roman to Integer

来源:互联网 发布:dc漫画软件 编辑:程序博客网 时间:2024/06/04 23:08

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 question.

public class Solution {    public int romanToInt(String s) {int sum = getValue(s.charAt(0));int last = sum;for (int i = 1; i < s.length(); ++i) {char temp = s.charAt(i);int val = getValue(temp);if (val > last)sum = sum - last + val - last;elsesum += val;last = val;}return sum;    }    static int getValue(char c) {switch (c) {case 'I':return 1;case 'X':return 10;case 'C':return 100;case 'M':return 1000;case 'V':return 5;case 'L':return 50;case 'D':return 500;default:return 1;}}}


0 0
原创粉丝点击