LeetCode 算法刷题(13)

来源:互联网 发布:安防网络系统 编辑:程序博客网 时间:2024/06/07 16:15

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.

public int romanToInt(String s) {        int result = 0;        int[] nums = {1000,500,100,50,10,5,1};        String chars = "MDCLXVI";        for(int i=0; i<s.length(); i++){            int index = chars.indexOf(s.charAt(i));            int nextindex = 0;            if(i==s.length()-1){                nextindex = chars.indexOf(s.charAt(i));            }else{                nextindex = chars.indexOf(s.charAt(i+1));            }            int thisInt = nums[index];            if(nums[index]<nums[nextindex]){                thisInt = nums[nextindex]-nums[index];                i++;            }            result = result+thisInt;        }        return result;    }






0 0
原创粉丝点击