13. Roman to Integer

13. Roman to Integer

class Solution {
    public int romanToInt(String s) {
        int out = 0;
        for (int i = 0; i <s>= 0 &amp;&amp; s.charAt(i-1) == 'I'){
                    out -= 1;
                    out += 4;
                }else out += 5;
            }
             //3
            else if (s.charAt(i) == 'X'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'I'){
                    out -= 1;
                    out += 9;
                }else out += 10;
            }
             //4
            else if (s.charAt(i) == 'L'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'X'){
                    out -= 10;
                    out += 40;
                }else out += 50;
            }
             //5
            else if (s.charAt(i) == 'C'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'X'){
                    out -= 10;
                    out += 90;
                }else out += 100;
            }
             //6
            else if (s.charAt(i) == 'D'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'C'){
                    out -= 100;
                    out += 400;
                }else out += 500;
            }
             //7
            else if (s.charAt(i) == 'M'){
                if ((i-1) &gt;= 0 &amp;&amp; s.charAt(i-1) == 'C'){
                    out -= 100;
                    out += 900;
                }else out += 1000;
            }
        }
        return out;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s