首页 > 其他 > 详细

[LeetCode#13] Roman to Integer

时间:2015-09-02 01:56:13      阅读:248      评论:0      收藏:0      [点我收藏+]

Problem:

Given a roman numeral, convert it to an integer.

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

Analysis:

This problem actually requires us to follow a given pattern to implement code.
Except some coding skills, the algorithm has already be given. 

Reference to Roman Numerals:
https://en.wikipedia.org/wiki/Roman_numerals

The max number the roman numeral stystem can represent is 3999. 
‘I‘ can represent range [1, 3]
add ‘V‘ can represent range [4, 8]

Unlike the numerial system, every position get number from [0, 9], but the weight for those position is differnt. 
99, the 9 at ‘10‘ digit means 90, however at ‘1‘ digit means 9.

The Roman numeral system exclude such representation. It represents number through "different characters" and relative position. 1. different characters. 
(‘I‘, 1); (‘V‘, 5); (‘X‘, 10); (‘L‘, 50); (‘C‘, 100); (‘D‘, 500); (‘M‘, 1000);

2. represent through characters combination for each digit in number. 
1954 as [M][CM][L][IV]
The character‘s value not based on position, but on itself!!!

What‘s more, the character is arranged from [high digit to low digit (in number form)]!

Note:
Each ‘1(000)‘ character could appear consecutively three times. the ‘5‘ character are not appear consecutively. 
Thus for each digit, we have following way to construct that digt, let us use digit 1 as example.
--------------------------------------------------------------------------------------------
I (1)
II (1+1)
III (1+1+1)
IV  (5-1)
V   (5)
VI  (5+1)
VII (5+2)
VIII (5+3)
IX  (10-1)
X   (10)
--------------------------------------------------------------------------------------------
Except ‘IV‘ and ‘IX‘, we could directly add the value of each character. Thus we could take this pattern into our programm!!!
for (int i = 0; i < s.length(); i++) {
    int cur = map.get(s.charAt(i));
    ret += cur;
    if (i >= 1) {
        int pre = map.get(s.charAt(i-1));
        if (pre*5 == cur || pre*10 == cur)
            ret -= 2*pre;
    }
}

Note: only when "if (pre*5 == cur || pre*10 == cur)"!!!
For each digit representation, only 3 characters could be appeared.
1 digit: I , V , X
10 digit: X, L, C
100 digit: C, D, M
1000 digit: M (that‘s why the maximum value is roman numerial system is : 3999)

Solution:

public class Solution {
    public int romanToInt(String s) {
        if (s == null || s.length() == 0)
            throw new IllegalArgumentException("The passed in arguments is illegal!");
        HashMap<Character, Integer> map = new HashMap<Character, Integer> ();
        map.put(‘I‘, 1);
        map.put(‘V‘, 5);
        map.put(‘X‘, 10);
        map.put(‘L‘, 50);
        map.put(‘C‘, 100);
        map.put(‘D‘, 500);
        map.put(‘M‘, 1000);
        int ret = 0;
        for (int i = 0; i < s.length(); i++) {
            int cur = map.get(s.charAt(i));
            ret += cur;
            if (i >= 1) {
                int pre = map.get(s.charAt(i-1));
                if (pre*5 == cur || pre*10 == cur)
                    ret -= 2*pre;
            }
        }
        return ret;
    }
}

 

[LeetCode#13] Roman to Integer

原文:http://www.cnblogs.com/airwindow/p/4777472.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!