{"id":99,"date":"2017-07-26T19:59:48","date_gmt":"2017-07-26T11:59:48","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=99"},"modified":"2018-11-27T16:34:27","modified_gmt":"2018-11-27T08:34:27","slug":"13-roman-to-integer","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=99","title":{"rendered":"13. Roman to Integer"},"content":{"rendered":"<p>Roman numerals are represented by seven different symbols:\u00a0<code>I<\/code>,\u00a0<code>V<\/code>,\u00a0<code>X<\/code>,\u00a0<code>L<\/code>,\u00a0<code>C<\/code>,\u00a0<code>D<\/code>\u00a0and\u00a0<code>M<\/code>.<\/p>\n<pre><strong>Symbol<\/strong>       <strong>Value<\/strong>\r\nI             1\r\nV             5\r\nX             10\r\nL             50\r\nC             100\r\nD             500\r\nM             1000<\/pre>\n<p>For example,\u00a0two is written as\u00a0<code>II<\/code>\u00a0in Roman numeral, just two one&#8217;s added together. Twelve is written as,\u00a0<code>XII<\/code>, which is simply\u00a0<code>X<\/code>\u00a0+\u00a0<code>II<\/code>. The number twenty seven is written as\u00a0<code>XXVII<\/code>, which is\u00a0<code>XX<\/code>\u00a0+\u00a0<code>V<\/code>\u00a0+\u00a0<code>II<\/code>.<\/p>\n<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not\u00a0<code>IIII<\/code>. Instead, the number four is written as\u00a0<code>IV<\/code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as\u00a0<code>IX<\/code>. There are six instances where subtraction is used:<\/p>\n<ul>\n<li><code>I<\/code>\u00a0can be placed before\u00a0<code>V<\/code>\u00a0(5) and\u00a0<code>X<\/code>\u00a0(10) to make 4 and 9.<\/li>\n<li><code>X<\/code>\u00a0can be placed before\u00a0<code>L<\/code>\u00a0(50) and\u00a0<code>C<\/code>\u00a0(100) to make 40 and 90.<\/li>\n<li><code>C<\/code>\u00a0can be placed before\u00a0<code>D<\/code>\u00a0(500) and\u00a0<code>M<\/code>\u00a0(1000) to make 400 and 900.<\/li>\n<\/ul>\n<p>Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre><strong>Input:<\/strong>\u00a0\"III\"\r\n<strong>Output:<\/strong> 3<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><strong>Input:<\/strong>\u00a0\"IV\"\r\n<strong>Output:<\/strong> 4<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre><strong>Input:<\/strong>\u00a0\"IX\"\r\n<strong>Output:<\/strong> 9<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre><strong>Input:<\/strong>\u00a0\"LVIII\"\r\n<strong>Output:<\/strong> 58\r\n<strong>Explanation:<\/strong> L = 50, V= 5, III = 3.\r\n<\/pre>\n<p><strong>Example 5:<\/strong><\/p>\n<pre><strong>Input:<\/strong>\u00a0\"MCMXCIV\"\r\n<strong>Output:<\/strong> 1994\r\n<strong>Explanation:<\/strong> M = 1000, CM = 900, XC = 90 and IV = 4.<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution:\r\n    # @param {string} s\r\n    # @return {integer}\r\n    def romanToInt(self, s):\r\n        roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}\r\n\r\n        if s == &quot;&quot;:\r\n            return 0\r\n        index = len(s) - 2\r\n        result = roman[s[-1]]\r\n        while index &gt;= 0:\r\n            if roman[s[index]] &lt; roman[s[index + 1]]:\r\n                result -= roman[s[index]]\r\n            else:\r\n                result += roman[s[index]]\r\n            index -= 1\r\n        return result\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Roman numerals are represented by seven different symbo<a href=\"https:\/\/www.ccagml.com\/?p=99\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">13. Roman to Integer<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}