java 93ms


  • 0
    A
      class Solution {		
    	public int romanToInt(String s) {
    		int result = 0;
    		int[] chars = new int[s.length()];
    		chars = setRomanSignArray(s);
    		
    		for (int i = 0; i < s.length() - 1; i++){
    			int curr = chars[i];
    			int next = chars[i+1];
    			if (curr < next) {
    				curr *= (-1);
    			}
    			result += curr;
    		}
    		
         result += chars[s.length()-1];
    		return result;
    	}
    
    	public int[] setRomanSignArray(String s){
    		int nums[]=new int[s.length()];
    		
    		for(int i=0; i<s.length(); i++){
    			switch (s.charAt(i)){
    			case 'M':
    				nums[i]=1000;
    				break;
    			case 'D':
    				nums[i]=500;
    				break;
    			case 'C':
    				nums[i]=100;
    				break;
    			case 'L':
    				nums[i]=50;
    				break;
    			case 'X' :
    				nums[i]=10;
    				break;
    			case 'V':
    				nums[i]=5;
    				break;
    			case 'I':
    				nums[i]=1;
    				break;
    			}
    		}
    		return nums;
     }	
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.