Thursday, October 10, 2013

Roman to Integer

Q:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
A:
public class Solution {
    public int romanToInt(String s) {
        StringBuffer buf = new StringBuffer(s);
        // deal with the case that we have 9's , went to 5-1-5
        if (buf.indexOf("IX") >= 0) {
            int i = buf.indexOf("IX");
            buf.replace(i, i + 2, "VIV");
        }
        if (buf.indexOf("XC") >= 0) {
            int i = buf.indexOf("XC");
            buf.replace(i, i + 2, "LXL");
        }
        if (buf.indexOf("CM") >= 0) {
            int i = buf.indexOf("CM");
            buf.replace(i, i + 2, "DCD");
        }
        // change 1-5 into 4's
        if (buf.indexOf("IV") >= 0) {
            int i = buf.indexOf("IV");
            buf.replace(i, i + 2, "IIII");
        }
        if (buf.indexOf("VX") >= 0) {
            int i = buf.indexOf("VX");
            buf.replace(i, i + 2, "VVVV");
        }
        if (buf.indexOf("XL") >= 0) {
            int i = buf.indexOf("XL");
            buf.replace(i, i + 2, "XXXX");
        }
        if (buf.indexOf("LC") >= 0) {
            int i = buf.indexOf("LC");
            buf.replace(i, i + 2, "LLLL");
        }
        if (buf.indexOf("CD") >= 0) {
            int i = buf.indexOf("CD");
            buf.replace(i, i + 2, "CCCC");
        }
        if (buf.indexOf("DM") >= 0) {
            int i = buf.indexOf("DM");
            buf.replace(i, i + 2, "DDDD");
        }
        // calculate them by adding
        int sum = 0;
        for (int i = 0; i < buf.length(); i++) {
            char ch = buf.charAt(i);
            switch (ch) {
            case 'M':
                sum += 1000;
                break;
            case 'D':
                sum += 500;
                break;
            case 'C':
                sum += 100;
                break;
            case 'L':
                sum += 50;
                break;
            case 'X':
                sum += 10;
                break;
            case 'V':
                sum += 5;
                break;
            case 'I':
                sum += 1;
                break;
            }
        }
        return sum;

    }
}


Mistakes:
1: switch  的case 完了,要记得用break语句。

Learned:

------------------第二遍--------用一个数组来取代那些字母,然后通过对比他们的大小关系来确定加还是减-------

public class Solution {
    public int romanToInt(String s) {
        int[] A = new int[s.length()];
        for(int i =0;i<s.length();i++){
            char ch = s.charAt(i);
            switch(ch){
                case 'M': A[i]=1000;
                break;
                case 'D': A[i]=500;
                break;
                case 'C': A[i]=100;
                break;
                case 'L': A[i]=50;
                break;
                case 'X': A[i]=10;
                break;
                case 'V': A[i]=5;
                break;
                case 'I': A[i]=1;
                break;
            }   
        }
        
        int sum = 0;
        for(int i =0;i<A.length;i++){
            if(i == A.length -1){// if it is the last one
                sum += A[i];
                break;
            }else{
                if(A[i] * 5 == A[i+1]  || A[i] * 10 == A[i+1] ){
                    sum -= A[i];
                    sum += A[i+1];
                    i++;
                }else{
                    sum += A[i];
                }
            }
        }
        return sum;
    }
}

------------------第二遍, 用HashMap来作---------------

public class Solution {
    public int romanToInt(String s) {
        if(s.length() ==0)
            return 0;
        StringBuffer buf = new StringBuffer(s);
        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 sum = map.get(s.charAt(0));
        for(int i=1;i<s.length();i++){
            sum += map.get(s.charAt(i));
            if(map.get(s.charAt(i)) > map.get(s.charAt(i-1)))
                sum -= 2* map.get(s.charAt(i-1));
        }
        return sum;
    }
}




No comments:

Post a Comment