13. Roman to Integer

Total Accepted: 71392
Total Submissions: 188974
Difficulty: Easy

Given a roman numeral, convert it to an integer.

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

Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Solution {
    public int romanToInt(String s) {
         int len=s.length()-1;
         int[] hash=new int[26];
         hash['I'-'A']=1;
         hash['V'-'A']=5;
         hash['X'-'A']=10;
         hash['L'-'A']=50;
         hash['C'-'A']=100;
         hash['D'-'A']=500;
         hash['M'-'A']=1000;
         int i=0,ans=0;
         for(;i<len;++i){
             if(hash[s.charAt(i)-'A']<hash[s.charAt(i+1)-'A'])
                 ans-=hash[s.charAt(i)-'A'];
             else
                 ans+=hash[s.charAt(i)-'A'];
         }
         return ans+hash[s.charAt(i)-'A'];

    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。