Skip to content

Instantly share code, notes, and snippets.

@0bach0
Last active February 16, 2020 15:52
Show Gist options
  • Select an option

  • Save 0bach0/2f844bc2532b40ebbeabcc9ff010e2b8 to your computer and use it in GitHub Desktop.

Select an option

Save 0bach0/2f844bc2532b40ebbeabcc9ff010e2b8 to your computer and use it in GitHub Desktop.

Revisions

  1. 0bach0 revised this gist Feb 16, 2020. No changes.
  2. 0bach0 created this gist Feb 16, 2020.
    45 changes: 45 additions & 0 deletions Roman2Integer.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    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;
    }
    }