{"id":4036,"date":"2018-09-19T08:39:39","date_gmt":"2018-09-19T15:39:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4036"},"modified":"2020-01-14T20:16:28","modified_gmt":"2020-01-15T04:16:28","slug":"leetcode-13-roman-to-integer","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-13-roman-to-integer\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 13. Roman to Integer"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 13. Roman to Integer - \u5237\u9898\u627e\u5de5\u4f5c EP299\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/joFpSO_sHnU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n<h1><strong>Problem<\/strong><\/h1>\n<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I<\/code>,&nbsp;<code>V<\/code>,&nbsp;<code>X<\/code>,&nbsp;<code>L<\/code>,&nbsp;<code>C<\/code>,&nbsp;<code>D<\/code>&nbsp;and&nbsp;<code>M<\/code>.<\/p>\n<pre class=\"crayon:false\"><strong>Symbol<\/strong>       <strong>Value<\/strong>\nI             1\nV             5\nX             10\nL             50\nC             100\nD             500\nM             1000<\/pre>\n<p>For example,&nbsp;two is written as&nbsp;<code>II<\/code>&nbsp;in Roman numeral, just two one&#8217;s added together. Twelve is written as,&nbsp;<code>XII<\/code>, which is simply&nbsp;<code>X<\/code>&nbsp;+&nbsp;<code>II<\/code>. The number twenty seven is written as&nbsp;<code>XXVII<\/code>, which is&nbsp;<code>XX<\/code>&nbsp;+&nbsp;<code>V<\/code>&nbsp;+&nbsp;<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&nbsp;<code>IIII<\/code>. Instead, the number four is written as&nbsp;<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&nbsp;<code>IX<\/code>. There are six instances where subtraction is used:<\/p>\n<ul>\n<li><code>I<\/code>&nbsp;can be placed before&nbsp;<code>V<\/code>&nbsp;(5) and&nbsp;<code>X<\/code>&nbsp;(10) to make 4 and 9.<\/li>\n<li><code>X<\/code>&nbsp;can be placed before&nbsp;<code>L<\/code>&nbsp;(50) and&nbsp;<code>C<\/code>&nbsp;(100) to make 40 and 90.<\/li>\n<li><code>C<\/code>&nbsp;can be placed before&nbsp;<code>D<\/code>&nbsp;(500) and&nbsp;<code>M<\/code>&nbsp;(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 class=\"crayon:false\"><strong>Input:<\/strong>&nbsp;\"III\"\n<strong>Output:<\/strong> 3<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>&nbsp;\"IV\"\n<strong>Output:<\/strong> 4<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>&nbsp;\"IX\"\n<strong>Output:<\/strong> 9<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>&nbsp;\"LVIII\"\n<strong>Output:<\/strong> 58\n<strong>Explanation:<\/strong> C = 100, L = 50, XXX = 30 and III = 3.\n<\/pre>\n<p><strong>Example 5:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>&nbsp;\"MCMXCIV\"\n<strong>Output:<\/strong> 1994\n<strong>Explanation:<\/strong> M = 1000, CM = 900, XC = 90 and IV = 4.<\/pre>\n<h1><strong>Solution<\/strong><\/h1>\n<p>accumulate the value of each letter.<\/p>\n<p>If the value of current letter is greater than the previous one, deduct twice of the previous value.<\/p>\n<p>e.g. IX, 1 + 10 &#8211; 2 * 1 = 9 instead of 1 + 10 = 11<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int romanToInt(string s) {\n    map&lt;char, int&gt; m{{'I', 1}, {'V', 5}, \n                     {'X', 10}, {'L', 50},\n                     {'C', 100}, {'D', 500},\n                     {'M', 1000}};\n    int ans = 0;\n    for (int i = 0; i &lt; s.length(); i++) {\n      ans += m[s[i]];\n      if (i &gt; 0 &amp;&amp; m[s[i]] &gt; m[s[i - 1]])\n        ans -= 2 * m[s[i - 1]];\n    }\n    return ans;\n  }\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\nclass Solution {\n  private int v(char R) {\n    switch (R) {\n      case 'I' : return 1;\n      case 'V' : return 5;\n      case 'X' : return 10;\n      case 'L' : return 50;\n      case 'C' : return 100;\n      case 'D' : return 500;\n      case 'M' : return 1000;\n      default: break;\n    }\n    return 0;\n  }\n  \n  public int romanToInt(String s) {\n    int ans = 0;\n    for (int i = 0; i &lt; s.length(); i++) {\n      ans += v(s.charAt(i));\n      if ( i &gt; 0 &amp;&amp; v(s.charAt(i)) &gt; v(s.charAt(i - 1)))        \n          ans -= 2 * v(s.charAt(i - 1));        \n    }\n    return ans;\n  }\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\">class Solution:\n  def romanToInt(self, s):\n    m = {'I' : 1, 'V': 5, 'X': 10, 'L' : 50,\n         'C' : 100, 'D': 500, 'M': 1000}\n    ans = m[s[0]]\n    for c, p in zip(s[1:], s[0:-1]):\n      ans += m[c]\n      if m[c] &gt; m[p]: ans -= 2 * m[p]\n    return ans<\/pre>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Problem Roman numerals are represented by seven different symbols:&nbsp;I,&nbsp;V,&nbsp;X,&nbsp;L,&nbsp;C,&nbsp;D&nbsp;and&nbsp;M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,47],"tags":[93,222,404,4],"class_list":["post-4036","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-conversion","tag-easy","tag-roman","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4036"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4036\/revisions"}],"predecessor-version":[{"id":6098,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4036\/revisions\/6098"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}