Last active
          February 16, 2020 15:52 
        
      - 
      
 - 
        
Save 0bach0/2f844bc2532b40ebbeabcc9ff010e2b8 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package jp.vietpro.vta; | |
| class Solution { | |
| public static int romanToInt(String s) { | |
| int value = 0; | |
| char prevValue = '0'; | |
| for (int i = 0; i < s.length(); i++) { | |
| if(i > 0) | |
| prevValue = s.charAt(i-1); | |
| switch (s.charAt(i)) { | |
| case 'V': | |
| value+=5; | |
| if(prevValue == 'I') value-=2; | |
| break; | |
| case 'X': | |
| value+=10; | |
| if(prevValue == 'I') value-=2; | |
| break; | |
| case 'L': | |
| value+=50; | |
| if(prevValue == 'X') value-=20; | |
| break; | |
| case 'C': | |
| value+=100; | |
| if(prevValue == 'X') value-=20; | |
| break; | |
| case 'D': | |
| value+=500; | |
| if(prevValue == 'C') value-=200; | |
| break; | |
| case 'M': | |
| value+=1000; | |
| if(prevValue == 'C') value-=200; | |
| break; | |
| case 'I': value+=1; | |
| } | |
| } | |
| return value; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment