{"id":459,"date":"2020-09-26T10:44:06","date_gmt":"2020-09-26T02:44:06","guid":{"rendered":"http:\/\/39.96.58.60\/?p=459"},"modified":"2022-10-18T16:39:14","modified_gmt":"2022-10-18T08:39:14","slug":"leetcode-12-%e6%95%b4%e6%95%b0%e8%bd%ac%e7%bd%97%e9%a9%ac%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=459","title":{"rendered":"Leetcode 12. \u6574\u6570\u8f6c\u7f57\u9a6c\u6570\u5b57"},"content":{"rendered":"<p>\u7f57\u9a6c\u6570\u5b57\u5305\u542b\u4ee5\u4e0b\u4e03\u79cd\u5b57\u7b26\uff1a&nbsp;<code>I<\/code>\uff0c&nbsp;<code>V<\/code>\uff0c&nbsp;<code>X<\/code>\uff0c&nbsp;<code>L<\/code>\uff0c<code>C<\/code>\uff0c<code>D<\/code>&nbsp;\u548c&nbsp;<code>M<\/code>\u3002<\/p>\n<pre><strong>\u5b57\u7b26<\/strong>          <strong>\u6570\u503c<\/strong>\n\nI             1\n\nV             5\n\nX             10\n\nL             50\n\nC             100\n\nD             500\n\nM             1000<\/pre>\n<p>\u4f8b\u5982\uff0c \u7f57\u9a6c\u6570\u5b57 2 \u5199\u505a&nbsp;<code>II<\/code>&nbsp;\uff0c\u5373\u4e3a\u4e24\u4e2a\u5e76\u5217\u7684 1\u300212 \u5199\u505a&nbsp;<code>XII<\/code>&nbsp;\uff0c\u5373\u4e3a&nbsp;<code>X<\/code>&nbsp;+&nbsp;<code>II<\/code>&nbsp;\u3002 27 \u5199\u505a&nbsp;&nbsp;<code>XXVII<\/code>, \u5373\u4e3a&nbsp;<code>XX<\/code>&nbsp;+&nbsp;<code>V<\/code>&nbsp;+&nbsp;<code>II<\/code>&nbsp;\u3002<\/p>\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7f57\u9a6c\u6570\u5b57\u4e2d\u5c0f\u7684\u6570\u5b57\u5728\u5927\u7684\u6570\u5b57\u7684\u53f3\u8fb9\u3002\u4f46\u4e5f\u5b58\u5728\u7279\u4f8b\uff0c\u4f8b\u5982 4 \u4e0d\u5199\u505a&nbsp;<code>IIII<\/code>\uff0c\u800c\u662f&nbsp;<code>IV<\/code>\u3002\u6570\u5b57 1 \u5728\u6570\u5b57 5 \u7684\u5de6\u8fb9\uff0c\u6240\u8868\u793a\u7684\u6570\u7b49\u4e8e\u5927\u6570 5 \u51cf\u5c0f\u6570 1 \u5f97\u5230\u7684\u6570\u503c 4 \u3002\u540c\u6837\u5730\uff0c\u6570\u5b57 9 \u8868\u793a\u4e3a&nbsp;<code>IX<\/code>\u3002\u8fd9\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\u53ea\u9002\u7528\u4e8e\u4ee5\u4e0b\u516d\u79cd\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li><code>I<\/code>&nbsp;\u53ef\u4ee5\u653e\u5728&nbsp;<code>V<\/code>&nbsp;(5) \u548c&nbsp;<code>X<\/code>&nbsp;(10) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 4 \u548c 9\u3002<\/li>\n<li><code>X<\/code>&nbsp;\u53ef\u4ee5\u653e\u5728&nbsp;<code>L<\/code>&nbsp;(50) \u548c&nbsp;<code>C<\/code>&nbsp;(100) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 40 \u548c&nbsp;90\u3002&nbsp;<\/li>\n<li><code>C<\/code>&nbsp;\u53ef\u4ee5\u653e\u5728&nbsp;<code>D<\/code>&nbsp;(500) \u548c&nbsp;<code>M<\/code>&nbsp;(1000) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a&nbsp;400 \u548c&nbsp;900\u3002<\/li>\n<\/ul>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\uff0c\u5c06\u5176\u8f6c\u4e3a\u7f57\u9a6c\u6570\u5b57\u3002\u8f93\u5165\u786e\u4fdd\u5728 1&nbsp;\u5230 3999 \u7684\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>&nbsp;3\n\n<strong>\u8f93\u51fa:<\/strong> \"III\"<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>&nbsp;4\n\n<strong>\u8f93\u51fa:<\/strong> \"IV\"<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;3:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>&nbsp;9\n\n<strong>\u8f93\u51fa:<\/strong> \"IX\"<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;4:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>&nbsp;58\n\n<strong>\u8f93\u51fa:<\/strong> \"LVIII\"\n\n<strong>\u89e3\u91ca:<\/strong> L = 50, V = 5, III = 3.\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;5:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>&nbsp;1994\n\n<strong>\u8f93\u51fa:<\/strong> \"MCMXCIV\"\n\n<strong>\u89e3\u91ca:<\/strong> M = 1000, CM = 900, XC = 90, IV = 4.<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u5b66\u3001 \u5b57\u7b26\u4e32\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a52 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8693.52% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8679.33% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u4f9d\u6b21\u53d6\u51fa\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d\u8fdb\u884c\u5224\u65ad\n    \u7f57\u9a6c\u6570\u5b57\u5206\u51e0\u79cd\u60c5\u51b5\uff1a\n        1~3:    III\n        4:      IV\n        5:      V\n        6-8:    VIII\n        9:      IX\n    \u7528\u5217\u8868\u5206\u522b\u5b58\u50a8 1 5 10\u5728\u4e0d\u540c\u4f4d\u4e0a\u7684\u8868\u793a\uff0c\u5373\uff1a[[1, 5, 10], [10, 50, 100], [100, 500, 1000], [1000]]\n    \u5728\u4e0d\u540c\u4f4d\u65f6\uff0c\u7528\u4e0d\u540c\u4f4d\u7684\u8868\u793a\u4f9d\u7167\u7f57\u9a6c\u6570\u5b57\u5206\u60c5\u51b5\u5904\u7406\u5373\u53ef\n\"\"\"\nclass Solution:\n    def intToRoman(self, num: int) -&gt; str:\n        roman_list = [\n            ['I', 'V', 'X',],\n            ['X', 'L', 'C'],\n            ['C', 'D', 'M'],\n            ['M']\n        ]\n        result = ''\n        i = 0\n        while num:\n            roman = roman_list[i]\n            num_ = num%10\n            if num_ &lt;= 3:\n                result = roman[0]*num_ + result\n            elif num_ == 4:\n                result = roman[0] + roman[1] + result\n            elif num_ == 5:\n                result = roman[1] + result\n\n            elif 5 &lt; num_ &lt;= 8:\n                result = roman[1] + roman[0]*(num_-5) + result\n\n            elif num_ == 9:\n                result = roman[0] + roman[2] + result\n\n            i += 1\n            num = num \/\/ 10\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7f57\u9a6c\u6570\u5b57\u5305\u542b\u4ee5\u4e0b\u4e03\u79cd\u5b57\u7b26\uff1a&nbsp;I\uff0c&nbsp;V\uff0c&nbsp;X\uff0c&nbsp;L\uff0cC\uff0cD&nbsp;\u548c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/459"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=459"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":460,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions\/460"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}