码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 13. Roman to Integer

时间:2018-04-28 14:16:08      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:first   ring   int   roman   array   public   分享   color   lse   

first:

class Solution {
    public int romanToInt(String s) {
        char[] charArray = s.toCharArray();
        int result =0;
        for(int i=0;i<charArray.length;i++){
            if(charArray[i]==‘I‘&&(charArray[i+1]==‘V‘||charArray[i+1]==‘X‘)){
                result--;
            }else if(charArray[i]==‘X‘&&(charArray[i+1]==‘L‘||charArray[i+1]==‘C‘)){
                result -=10;
            }else if(charArray[i]==‘C‘&&(charArray[i+1]==‘D‘||charArray[i+1]==‘M‘)){
                result -=100;
            }else{
                switch (charArray[i]){
                case ‘I‘:result++;
                         break;
                case ‘V‘:result+=5;
                         break;
                case ‘X‘:result+=10;
                         break;
                case ‘L‘:result+=50;
                         break;
                case ‘C‘:result+=100;
                         break;
                case ‘D‘:result+=500;
                         break;
                case ‘M‘:result+=1000;
                }        
            }            
        }
        return result;
    }
}

 

result:

Run Code Status: Runtime Error
Run Code Result:
Your input

"III"

Your answer

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Solution.romanToInt(Solution.java:6)
    at __DriverSolution__.__helper__(__Driver__.java:8)
    at __Driver__.main(__Driver__.java:52)

second:

class Solution {
    public int romanToInt(String s) {
        char[] charArray = s.toCharArray();
        int result =0;
        for(int i=0;i<charArray.length;i++){
            if(charArray[i]==‘I‘&&i+1<charArray.length&&(charArray[i+1]==‘V‘||charArray[i+1]==‘X‘)){
                result--;
            }else if(charArray[i]==‘X‘&&i+1<charArray.length&&(charArray[i+1]==‘L‘||charArray[i+1]==‘C‘)){
                result -=10;
            }else if(charArray[i]==‘C‘&&i+1<charArray.length&&(charArray[i+1]==‘D‘||charArray[i+1]==‘M‘)){
                result -=100;
            }else{
                switch (charArray[i]){
                case ‘I‘:result++;
                         break;
                case ‘V‘:result+=5;
                         break;
                case ‘X‘:result+=10;
                         break;
                case ‘L‘:result+=50;
                         break;
                case ‘C‘:result+=100;
                         break;
                case ‘D‘:result+=500;
                         break;
                case ‘M‘:result+=1000;
                }        
            }            
        }
        return result;
    }
}

 

result:

技术分享图片

 

conclusion:

 

LeetCode 13. Roman to Integer

标签:first   ring   int   roman   array   public   分享   color   lse   

原文地址:https://www.cnblogs.com/hzg1981/p/8966487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!