{"id":362,"date":"2025-07-02T15:38:04","date_gmt":"2025-07-02T07:38:04","guid":{"rendered":"http:\/\/getzs.online\/?p=362"},"modified":"2025-07-02T15:38:04","modified_gmt":"2025-07-02T07:38:04","slug":"13-%e7%bd%97%e9%a9%ac%e6%95%b0%e5%ad%97%e8%bd%ac%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/07\/02\/13-%e7%bd%97%e9%a9%ac%e6%95%b0%e5%ad%97%e8%bd%ac%e6%95%b4%e6%95%b0\/","title":{"rendered":"13. \u7f57\u9a6c\u6570\u5b57\u8f6c\u6574\u6570"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/roman-to-integer\/\">13. \u7f57\u9a6c\u6570\u5b57\u8f6c\u6574\u6570<\/a><\/p>\n\n\n\n<p>\u7f57\u9a6c\u6570\u5b57\u5305\u542b\u4ee5\u4e0b\u4e03\u79cd\u5b57\u7b26:&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\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u5b57\u7b26<\/strong>          <strong>\u6570\u503c<\/strong>\nI             1\nV             5\nX             10\nL             50\nC             100\nD             500\nM             1000<\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c \u7f57\u9a6c\u6570\u5b57&nbsp;<code>2<\/code>&nbsp;\u5199\u505a&nbsp;<code>II<\/code>&nbsp;\uff0c\u5373\u4e3a\u4e24\u4e2a\u5e76\u5217\u7684 1 \u3002<code>12<\/code>&nbsp;\u5199\u505a&nbsp;<code>XII<\/code>&nbsp;\uff0c\u5373\u4e3a&nbsp;<code>X<\/code>&nbsp;+&nbsp;<code>II<\/code>&nbsp;\u3002&nbsp;<code>27<\/code>&nbsp;\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\n\n\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\n\n\n<ul class=\"wp-block-list\">\n<li><code>I<\/code>\u00a0\u53ef\u4ee5\u653e\u5728\u00a0<code>V<\/code>\u00a0(5) \u548c\u00a0<code>X<\/code>\u00a0(10) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 4 \u548c 9\u3002<\/li>\n\n\n\n<li><code>X<\/code>\u00a0\u53ef\u4ee5\u653e\u5728\u00a0<code>L<\/code>\u00a0(50) \u548c\u00a0<code>C<\/code>\u00a0(100) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 40 \u548c\u00a090\u3002\u00a0<\/li>\n\n\n\n<li><code>C<\/code>\u00a0\u53ef\u4ee5\u653e\u5728\u00a0<code>D<\/code>\u00a0(500) \u548c\u00a0<code>M<\/code>\u00a0(1000) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a\u00a0400 \u548c\u00a0900\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u7f57\u9a6c\u6570\u5b57\uff0c\u5c06\u5176\u8f6c\u6362\u6210\u6574\u6570\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong>&nbsp;s = \"III\"\n<strong>\u8f93\u51fa:<\/strong> 3<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong>&nbsp;s = \"IV\"\n<strong>\u8f93\u51fa:<\/strong> 4<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong>&nbsp;s = \"IX\"\n<strong>\u8f93\u51fa:<\/strong> 9<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong>&nbsp;s = \"LVIII\"\n<strong>\u8f93\u51fa:<\/strong> 58\n<strong>\u89e3\u91ca:<\/strong> L = 50, V= 5, III = 3.\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong>&nbsp;s = \"MCMXCIV\"\n<strong>\u8f93\u51fa:<\/strong> 1994\n<strong>\u89e3\u91ca:<\/strong> M = 1000, CM = 900, XC = 90, IV = 4.<\/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;= s.length &lt;= 15<\/code><\/li>\n\n\n\n<li><code>s<\/code>\u00a0\u4ec5\u542b\u5b57\u7b26\u00a0<code>('I', 'V', 'X', 'L', 'C', 'D', 'M')<\/code><\/li>\n\n\n\n<li>\u9898\u76ee\u6570\u636e\u4fdd\u8bc1\u00a0<code>s<\/code>\u00a0\u662f\u4e00\u4e2a\u6709\u6548\u7684\u7f57\u9a6c\u6570\u5b57\uff0c\u4e14\u8868\u793a\u6574\u6570\u5728\u8303\u56f4\u00a0<code>[1, 3999]<\/code>\u00a0\u5185<\/li>\n\n\n\n<li>\u9898\u76ee\u6240\u7ed9\u6d4b\u8bd5\u7528\u4f8b\u7686\u7b26\u5408\u7f57\u9a6c\u6570\u5b57\u4e66\u5199\u89c4\u5219\uff0c\u4e0d\u4f1a\u51fa\u73b0\u8de8\u4f4d\u7b49\u60c5\u51b5\u3002<\/li>\n\n\n\n<li>IL \u548c IM \u8fd9\u6837\u7684\u4f8b\u5b50\u5e76\u4e0d\u7b26\u5408\u9898\u76ee\u8981\u6c42\uff0c49 \u5e94\u8be5\u5199\u4f5c XLIX\uff0c999 \u5e94\u8be5\u5199\u4f5c CMXCIX \u3002<\/li>\n\n\n\n<li>\u5173\u4e8e\u7f57\u9a6c\u6570\u5b57\u7684\u8be6\u5c3d\u4e66\u5199\u89c4\u5219\uff0c\u53ef\u4ee5\u53c2\u8003\u00a0<a href=\"https:\/\/baike.baidu.com\/item\/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97\/772296\">\u7f57\u9a6c\u6570\u5b57 &#8211; \u767e\u5ea6\u767e\u79d1<\/a>\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    HashMap&lt;Character, Integer> map = new HashMap&lt;>() {{\n        put('I', 1);\n        put('V', 5);\n        put('X', 10);\n        put('L', 50);\n        put('C', 100);\n        put('D', 500);\n        put('M', 1000);\n    }};\n\n    public int romanToInt(String s) {\n        return method1(s);\n    }\n\n\n    \/**\n     * \u6a21\u62df\n     * 5 ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method1(String s) {\n        char&#91;] chr = s.toCharArray();\n        int sum = 0;\n        int n = chr.length;\n        for (int i = 0; i &lt; n; i++) {\n            char c = chr&#91;i];\n            \/\/ \u524d\u9762\u5c0f\u4e8e\u540e\u9762\u5219\u51cf\u6cd5\uff0c\u5426\u5219\u52a0\u6cd5\n            if (i &lt; n - 1 &amp;&amp; map.get(c) &lt; map.get(chr&#91;i + 1])) {\n                sum -= map.get(c);\n            }\n            else {\n                sum += map.get(c);\n            }\n        }\n        return sum;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>13. \u7f57\u9a6c\u6570\u5b57\u8f6c\u6574\u6570 \u7f57\u9a6c\u6570\u5b57\u5305\u542b\u4ee5\u4e0b\u4e03\u79cd\u5b57\u7b26:&nbsp;I\uff0c&nbsp;V\uff0c&nbsp;X\uff0c&nbsp; [&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-362","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/362","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=362"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":363,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/362\/revisions\/363"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}