{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "810f4644697941d9894b88099b1bd39c",
  "keywords": "哈希表,数学,字符串",
  "title": "罗马数字转整数",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>罗马数字包含以下七种字符：&nbsp;<code>I</code>，&nbsp;<code>V</code>，&nbsp;<code>X</code>，&nbsp;<code>L</code>，<code>C</code>，<code>D</code>&nbsp;和&nbsp;<code>M</code>。\n</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>字符</strong>          <strong>数值</strong>\nI             1\nV             5\nX             10\nL             50\nC             100\nD             500\nM             1000</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>例如， 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;，即为两个并列的 1。12\n写做&nbsp;<code>XII</code>&nbsp;，即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27\n写做&nbsp;&nbsp;<code>XXVII</code>,\n即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>通常情况下，罗马数字中小的数字在大的数字的右边。但也存在特例，例如 4 不写做&nbsp;<code>IIII</code>，而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边，所表示的数等于大数 5\n减小数 1 得到的数值 4 。同样地，数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边，来表示 4 和 9。</li>\n<li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边，来表示 40\n和&nbsp;90。&nbsp;</li>\n<li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边，来表示&nbsp;400\n和&nbsp;900。</li>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>给你一个整数，将其转为罗马数字。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;1:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 3\n<strong><br />输出:</strong> \"III\"</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;2:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 4\n<strong><br />输出:</strong> \"IV\"</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;3:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 9\n<strong><br />输出:</strong> \"IX\"</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;4:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 58\n<strong><br />输出:</strong> \"LVIII\"\n<strong><br />解释:</strong> L = 50, V = 5, III = 3.\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;5:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>&nbsp;num = 1994\n<strong><br />输出:</strong> \"MCMXCIV\"\n<strong><br />解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>1 &lt;= num &lt;= 3999</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution:\n\tdef romanToInt(self, s: str) -> int:\n\t\tunits = [\n\t\t\t[\"M\", 1000, 1],\n\t\t\t[\"CM\", 900, 2],\n\t\t\t[\"D\", 500, 1],\n\t\t\t[\"CD\", 400, 2],\n\t\t\t[\"C\", 100, 1],\n\t\t\t[\"XC\", 90, 2],\n\t\t\t[\"L\", 50, 1],\n\t\t\t[\"XL\", 40, 2],\n\t\t\t[\"X\", 10, 1],\n\t\t\t[\"IX\", 9, 2],\n\t\t\t[\"V\", 5, 1],\n\t\t\t[\"IV\", 4, 2],\n\t\t\t[\"I\", 1, 1]\n\t\t]\n\t\tend = len(s)\n\t\tstart = 0\n\t\ti = 0\n\t\tr = 0\n\t\twhile i < len(units):\n\t\t\tunit = units[i][0]\n\t\t\tvalue = units[i][1]\n\t\t\tstep = units[i][2]\n\t\t\tif end-start >= step and s[start:start+step] == unit:\n\t\t\t\tr += value\n\t\t\t\tstart += step\n\t\t\telse:\n\t\t\t\ti += 1\n\t\treturn r\n# %%\ns = Solution()\nprint(s.romanToInt(\"III\"))",
    "language": "python"
  },
  "node_id": "dailycode-97cf74ce017444f1aa65cfa6a8ae7451",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600470218"
}