(Java)LeetCode-13. Roman to Integer

Given a roman numeral, convert it to an integer.

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



这一题和上一题姊妹题~同样的方法,反过来即可,easy~



public class Solution {
    public int romanToInt(String s) {
        int ans = 0;
		int[] aArray = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
	    String[] rArray = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
	    
	    for(int i = 0; i < rArray.length; i++){
	    	while(s.indexOf(rArray[i] )== 0){
	    		ans += aArray[i];
	    		s = s.substring(rArray[i].length());
	    	}
	    }
	    return ans;
    }
}





你可能感兴趣的:(JAVA,LeetCode)