{"id":780,"date":"2022-07-10T09:29:44","date_gmt":"2022-07-10T09:29:44","guid":{"rendered":"https:\/\/blog.liuyingjie.com.cn\/?p=780"},"modified":"2023-08-13T08:11:36","modified_gmt":"2023-08-13T08:11:36","slug":"13-roman-to-integer","status":"publish","type":"post","link":"https:\/\/blog.liuyingjie.com.cn\/?p=780","title":{"rendered":"13. \u7f57\u9a6c\u6570\u5b57\u8f6c\u6574\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u65b9\u6cd5\u4e00\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li> <span style=\"font-family: Georgia, Times, &quot;Times New Roman&quot;, serif; font-size: revert;\">\u521b\u5efa\u4e00\u4e2a\u54c8\u5e0c\u8868\u5c06\u7f57\u9a6c\u5b57\u7b26\u4e0e\u5bf9\u5e94\u7684\u6570\u503c\u5173\u8054\u8d77\u6765\u3002<\/span> <\/li>\n\n\n\n<li> <span style=\"font-family: Georgia, Times, &quot;Times New Roman&quot;, serif; font-size: revert;\">\u7136\u540e\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u904d\u5386\uff0c\u7531\u4e8e\u7ec4\u5408\u53ea\u6709\u4e24\u79cd\u60c5\u51b5\uff0c\u4e00\u79cd\u662f\u4e00\u4e2a\u5b57\u7b26\uff0c\u4e00\u79cd\u662f\u4e24\u4e2a\u5b57\u7b26\uff0c\u5176\u4e2d\u4e24\u4e2a\u5b57\u7b26\u4f18\u5148\u4e8e\u4e00\u4e2a\u5b57\u7b26<\/span> <\/li>\n\n\n\n<li> <span style=\"font-family: Georgia, Times, &quot;Times New Roman&quot;, serif; font-size: revert;\">\u5148\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26\u7684\u7ec4\u5408\u5728\u54c8\u5e0c\u8868\u4e2d\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5219\u5c06\u503c\u53d6\u51fa\u52a0\u5230\u7ed3\u679c<code>ans<\/code>\u4e2d\uff0c\u5e76\u5411\u540e\u79fb\u4e24\u4e2a\u5b57\u7b26\u3002\u4e0d\u5b58\u5728\u5219\u5224\u65ad\u5f53\u524d1\u4e2a\u5b57\u7b26\u662f\u5426\u5b58\u5728\uff0c\u5b58\u5728\u5219\u5c06\u503c\u53bb\u9664\u52a0\u5230\u7ed3\u679c<code>ans<\/code>\u4e2d\uff0c\u5e76\u5411\u540e\u79fb1\u4e2a\u5b57\u7b26\uff0c\u6700\u540e\u904d\u5386\u7ed3\u675f\u8fd4\u56de\u7ed3\u679c<\/span><code><span style=\"font-family: Georgia, Times, &quot;Times New Roman&quot;, serif; font-size: revert;\">ans<\/span> <\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">C \u4ee3\u7801\u9898\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">int romanToInt(char* s) {\n    int symbolValues[26];\n    symbolValues['I' - 'A'] = 1;\n    symbolValues['V' - 'A'] = 5;\n    symbolValues['X' - 'A'] = 10;\n    symbolValues['L' - 'A'] = 50;\n    symbolValues['C' - 'A'] = 100;\n    symbolValues['D' - 'A'] = 500;\n    symbolValues['M' - 'A'] = 1000;\n    int ans = 0;\n    int n = strlen(s);\n    for (int i = 0; i &lt; n; ++i) {\n        int value = symbolValues[s[i] - 'A'];\n        if (i &lt; n - 1 &amp;&amp; value &lt; symbolValues[s[i + 1] - 'A']) {\n            ans -= value;\n        } else {\n            ans += value;\n        }\n    }\n    return ans;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Python3 \u4ee3\u7801\u9898\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">class Solution:\n\n    SYMBOL_VALUES =  {\n        'I': 1,\n        'V': 5,\n        'X': 10,\n        'L': 50,\n        'C': 100,\n        'D': 500,\n        'M': 1000,\n    }\n\n    def romanToInt(self, s: str) -&gt; int:\n        ans = 0\n        n = len(s)\n        for i, ch in enumerate(s):\n            value = Solution.SYMBOL_VALUES[ch]\n            \/\/\u5224\u65ad\u5f53\u524d\u5b57\u7b26\u662f\u5426\u662f\u7279\u6b8a\u60c5\u51b5\uff0c\u5373\u5f53\u524d\u5b57\u7b26\u7684\u6570\u503c\u5c0f\u4e8e\u4e0b\u4e00\u4e2a\u5b57\u7b26\u7684\u6570\u503c\u3002\u5982\u679c\u662f\u7279\u6b8a\u60c5\u51b5\uff0c\u9700\u8981\u51cf\u53bb\u5f53\u524d\u5b57\u7b26\u7684\u6570\u503c\u3002\n            if i &lt; n - 1 and value &lt; Solution.SYMBOL_VALUES[s[i + 1]]:\n                ans -= value\n            else:\n                ans += value \n        return ans<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.liuyingjie.com.cn\/wp-content\/uploads\/2022\/07\/13.\u7f57\u9a6c\u6570\u5b57\u8f6c\u6574\u6570.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Source: LeetCode\uff08The title reproduced in this blog is for personal study use only\uff09<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u4e00\uff1a C \u4ee3\u7801\u9898\u89e3 Python3 \u4ee3\u7801\u9898\u89e3 Source: LeetCode\uff08The title rep &hellip; <a href=\"https:\/\/blog.liuyingjie.com.cn\/?p=780\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">13. \u7f57\u9a6c\u6570\u5b57\u8f6c\u6574\u6570<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-780","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=780"}],"version-history":[{"count":12,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions"}],"predecessor-version":[{"id":989,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions\/989"}],"wp:attachment":[{"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.liuyingjie.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}