{"id":365,"date":"2025-07-03T22:59:24","date_gmt":"2025-07-03T14:59:24","guid":{"rendered":"http:\/\/getzs.online\/?p=365"},"modified":"2025-07-03T22:59:24","modified_gmt":"2025-07-03T14:59:24","slug":"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":"https:\/\/hello.getzs.online\/index.php\/2025\/07\/03\/12-%e6%95%b4%e6%95%b0%e8%bd%ac%e7%bd%97%e9%a9%ac%e6%95%b0%e5%ad%97\/","title":{"rendered":"12. \u6574\u6570\u8f6c\u7f57\u9a6c\u6570\u5b57"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/integer-to-roman\/\">12. \u6574\u6570\u8f6c\u7f57\u9a6c\u6570\u5b57<\/a><\/p>\n\n\n\n<p>\u4e03\u4e2a\u4e0d\u540c\u7684\u7b26\u53f7\u4ee3\u8868\u7f57\u9a6c\u6570\u5b57\uff0c\u5176\u503c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7b26\u53f7<\/th><th>\u503c<\/th><\/tr><\/thead><tbody><tr><td>I<\/td><td>1<\/td><\/tr><tr><td>V<\/td><td>5<\/td><\/tr><tr><td>X<\/td><td>10<\/td><\/tr><tr><td>L<\/td><td>50<\/td><\/tr><tr><td>C<\/td><td>100<\/td><\/tr><tr><td>D<\/td><td>500<\/td><\/tr><tr><td>M<\/td><td>1000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7f57\u9a6c\u6570\u5b57\u662f\u901a\u8fc7\u6dfb\u52a0\u4ece\u6700\u9ad8\u5230\u6700\u4f4e\u7684\u5c0f\u6570\u4f4d\u503c\u7684\u8f6c\u6362\u800c\u5f62\u6210\u7684\u3002\u5c06\u5c0f\u6570\u4f4d\u503c\u8f6c\u6362\u4e3a\u7f57\u9a6c\u6570\u5b57\u6709\u4ee5\u4e0b\u89c4\u5219\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u8be5\u503c\u4e0d\u662f\u4ee5 4 \u6216 9 \u5f00\u5934\uff0c\u8bf7\u9009\u62e9\u53ef\u4ee5\u4ece\u8f93\u5165\u4e2d\u51cf\u53bb\u7684\u6700\u5927\u503c\u7684\u7b26\u53f7\uff0c\u5c06\u8be5\u7b26\u53f7\u9644\u52a0\u5230\u7ed3\u679c\uff0c\u51cf\u53bb\u5176\u503c\uff0c\u7136\u540e\u5c06\u5176\u4f59\u90e8\u5206\u8f6c\u6362\u4e3a\u7f57\u9a6c\u6570\u5b57\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8be5\u503c\u4ee5 4 \u6216 9 \u5f00\u5934\uff0c\u4f7f\u7528\u00a0<strong>\u51cf\u6cd5\u5f62\u5f0f<\/strong>\uff0c\u8868\u793a\u4ece\u4ee5\u4e0b\u7b26\u53f7\u4e2d\u51cf\u53bb\u4e00\u4e2a\u7b26\u53f7\uff0c\u4f8b\u5982\u00a04 \u662f 5 (<code>V<\/code>) \u51cf 1 (<code>I<\/code>):\u00a0<code>IV<\/code>\u00a0\uff0c9 \u662f 10 (<code>X<\/code>) \u51cf\u00a01 (<code>I<\/code>)\uff1a<code>IX<\/code>\u3002\u4ec5\u4f7f\u7528\u4ee5\u4e0b\u51cf\u6cd5\u5f62\u5f0f\uff1a4 (<code>IV<\/code>)\uff0c9 (<code>IX<\/code>)\uff0c40 (<code>XL<\/code>)\uff0c90 (<code>XC<\/code>)\uff0c400 (<code>CD<\/code>) \u548c\u00a0900 (<code>CM<\/code>)\u3002<\/li>\n\n\n\n<li>\u53ea\u6709 10 \u7684\u6b21\u65b9\uff08<code>I<\/code>,\u00a0<code>X<\/code>,\u00a0<code>C<\/code>,\u00a0<code>M<\/code>\uff09\u6700\u591a\u53ef\u4ee5\u8fde\u7eed\u9644\u52a0 3 \u6b21\u4ee5\u4ee3\u8868 10 \u7684\u500d\u6570\u3002\u4f60\u4e0d\u80fd\u591a\u6b21\u9644\u52a0\u00a05\u00a0(<code>V<\/code>)\uff0c50 (<code>L<\/code>) \u6216 500 (<code>D<\/code>)\u3002\u5982\u679c\u9700\u8981\u5c06\u7b26\u53f7\u9644\u52a04\u6b21\uff0c\u8bf7\u4f7f\u7528\u00a0<strong>\u51cf\u6cd5\u5f62\u5f0f<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u7f57\u9a6c\u6570\u5b57\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>num = 3749<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>&nbsp;&#8220;MMMDCCXLIX&#8221;<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">3000 = MMM \u7531\u4e8e 1000 (M) + 1000 (M) + 1000 (M)\n 700 = DCC \u7531\u4e8e 500 (D) + 100 (C) + 100 (C)\n  40 = XL \u7531\u4e8e 50 (L) \u51cf 10 (X)\n   9 = IX \u7531\u4e8e 10 (X) \u51cf 1 (I)\n\u6ce8\u610f\uff1a49 \u4e0d\u662f 50 (L) \u51cf 1 (I) \u56e0\u4e3a\u8f6c\u6362\u662f\u57fa\u4e8e\u5c0f\u6570\u4f4d\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>num = 58<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>&#8220;LVIII&#8221;<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">50 = L\n 8 = VIII\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u8f93\u5165\uff1a<\/strong>num = 1994<\/p>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong>&#8220;MCMXCIV&#8221;<\/p>\n\n\n\n<p><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1000 = M\n 900 = CM\n  90 = XC\n   4 = IV\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= num &lt;= 3999<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ \u521b\u5efa\u4e00\u4e2a\u6709\u5e8f\u7684map\uff0ckey\u4e3a\u6570\u5b57\uff0cvalue\u4e3a\u7f57\u9a6c\u6570\u5b57, key\u503c\u964d\u5e8f\u6392\u5217\n    public static TreeMap&lt;Integer, String> map = new TreeMap&lt;>(new Comparator&lt;Integer>() {\n        @Override\n        public int compare(Integer t1, Integer t2) {\n            return t2.compareTo(t1);\n        }\n    });\n\n    static {\n        map.put(1, \"I\");\n        map.put(5, \"V\");\n        map.put(10, \"X\");\n        map.put(50, \"L\");\n        map.put(100, \"C\");\n        map.put(500, \"D\");\n        map.put(1000, \"M\");\n\n        map.put(4, \"IV\");\n        map.put(9, \"IX\");\n        map.put(40, \"XL\");\n        map.put(90, \"XC\");\n        map.put(400, \"CD\");\n        map.put(900, \"CM\");\n    }\n\n    public String intToRoman(int num) {\n        return method1(num);\n    }\n\n    public String method1(int num) {\n        StringBuilder sb = new StringBuilder();\n        for (int key : map.keySet()) {\n            String value = map.get(key);\n            while (num >= key) {\n                sb.append(value);\n                num -= key;\n            }\n        }\n        return sb.toString();\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>12. \u6574\u6570\u8f6c\u7f57\u9a6c\u6570\u5b57 \u4e03\u4e2a\u4e0d\u540c\u7684\u7b26\u53f7\u4ee3\u8868\u7f57\u9a6c\u6570\u5b57\uff0c\u5176\u503c\u5982\u4e0b\uff1a \u7b26\u53f7 \u503c I 1 V 5 X 10 L 50  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,11],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":366,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/365\/revisions\/366"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}