{"id":1957,"date":"2022-05-10T15:38:34","date_gmt":"2022-05-10T15:38:34","guid":{"rendered":"https:\/\/www.zttofficial.com\/?p=1957"},"modified":"2022-05-10T17:00:40","modified_gmt":"2022-05-10T17:00:40","slug":"%e5%81%9a%e9%a1%8c%e7%ad%86%e8%a8%98%ef%bc%9aroman-to-integer-java","status":"publish","type":"post","link":"https:\/\/www.zttofficial.com\/?p=1957","title":{"rendered":"\u505a\u984c\u7b46\u8a18\uff1aRoman to Integer (Java)"},"content":{"rendered":"\n<p><strong>\u984c\u76ee\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<p>\u5c07\u7f85\u99ac\u6578\u5b57\u8f49\u63db\u70ba\u6574\u6578\uff0c\u5177\u9ad4\u898f\u5247\u7565\u3002<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: s = \"III\"\nOutput: 3\nExplanation: III = 3.<\/code><\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: s = \"LVIII\"\nOutput: 58\nExplanation: L = 50, V= 5, III = 3.<\/code><\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Input: s = \"MCMXCIV\"\nOutput: 1994\nExplanation: M = 1000, CM = 900, XC = 90 and IV = 4.<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u81ea\u5df1\u7684 Switch \u66b4\u529b\u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p>\u515c\u4e86\u597d\u5e7e\u500b\u5708\u2026\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">class Solution {\n    public int romanToInt(String s) {\n        int num = 0;\n        StringBuilder re = new StringBuilder();\n        re.append(s).reverse();\n        String[] split = re.toString().split(\"(?&lt;=\\\\G..)\");\n        for (String i:split){\n            switch(i){\n                case \"VI\":\n                    num = num + 4;\n                    break;\n                case \"XI\":\n                    num = num + 9;\n                    break;\n                case \"LX\":\n                    num = num + 40; \n                    break;\n                case \"CX\":\n                    num = num + 90; \n                    break;\n                case \"DC\":\n                    num = num + 400;\n                    break;\n                case \"MC\":\n                    num = num + 900; \n                    break;\n                default:\n                    String[] sub = i.split(\"\");\n                    for (String j:sub){\n                        switch(j){\n                            case \"I\":\n                                num = num + 1;\n                                break;\n                            case \"V\":\n                                num = num + 5;\n                                break;\n                            case \"X\":\n                                num = num + 10; \n                                break;\n                            case \"L\":\n                                num = num + 50; \n                                break;\n                            case \"C\":\n                                num = num + 100;\n                                break;\n                            case \"D\":\n                                num = num + 500; \n                                break;\n                            case \"M\":\n                                num = num + 1000;     \n                                break;\n                        }\n                    }\n                }\n        }\n        return num;    \n    }\n}<\/code><\/pre>\n\n\n\n<p>\u610f\u5916\u767c\u751f\u4e86\uff0c\u672c\u5730 IDE \u80fd\u8dd1\u51fa\u6b63\u78ba\u7b54\u6848\uff0c\u4f46\u662f\u5728 Leetcode \u4e0a\u8dd1\u4e0d\u51fa\u4f86\uff0c\u6211\u7adf\u7136\u70ba\u9019\u7a2e\u554f\u984c\u6d6a\u8cbb\u4e86\u4e0d\u5c11\u6642\u9593\uff0c\u6700\u7d42\u4e5f\u6c92\u89e3\u6c7a\u2026\u2026<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u6b63\u78ba\u7cbe\u7df4\u7684 Switch \u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">class Solution {\n\t public int romanToInt(String s) {\n\t    int nums[] = new int[s.length()];\n\t    for(int i = 0; i &lt; s.length(); i++){\n\t        switch (s.charAt(i)) {\n\t            case 'M':\n\t                nums[i] = 1000;\n\t                break;\n\t            case 'D':\n\t                nums[i] = 500;\n\t                break;\n\t            case 'C':\n\t                nums[i] = 100;\n\t                break;\n\t            case 'L':\n\t                nums[i] = 50;\n\t                break;\n\t            case 'X' :\n\t                nums[i] = 10;\n\t                break;\n\t            case 'V':\n\t                nums[i] = 5;\n\t                break;\n\t            case 'I':\n\t                nums[i] = 1;\n\t                break;\n\t        }\n\t    } \n\t    \n\t    int sum = 0;\n\t    for(int i=0; i&lt;nums.length-1; i++){\n\t        if(nums[i] &lt; nums[i+1])\n\t            sum -= nums[i];\n\t        else\n\t            sum += nums[i];\n\t    }\n         \n\t    return sum + nums[nums.length-1];\n\t}\n}<\/code><\/pre>\n\n\n\n<p>6 ms<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u904b\u7528 <strong>Hashmap\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\">class solution {\n    public int romanToInt(String s) {\n        Map map = new HashMap&lt;&gt;();\n        map.put('I',1);\n        map.put('V',5);\n        map.put('X',10);\n        map.put('L',50);\n        map.put('C',100);\n        map.put('D',500);\n        map.put('M',1000);\n        \n        int result = map.get(s.charAt(s.length()-1));\n        for(int i=s.length()-2; i&gt;=0; i--){\n            if(map.get(s.charAt(i)) &lt; map.get(s.charAt(i+1))){\n                result -= map.get(s.charAt(i));\n            }\n            else{\n                result += map.get(s.charAt(i));\n            }\n        }\n        return result;\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u984c\u76ee\u63cf\u8ff0\uff1a \u5c07\u7f85\u99ac\u6578\u5b57\u8f49\u63db\u70ba\u6574\u6578\uff0c\u5177\u9ad4\u898f\u5247\u7565\u3002 Example 1: Example 2: Example 3: \u81ea\u5df1\u7684 Switch \u66b4\u529b\u89e3\u6cd5\uff1a \u515c\u4e86\u597d\u5e7e\u500b\u5708\u2026\u2026 \u610f\u5916\u767c\u751f\u4e86\uff0c\u672c\u5730 IDE \u80fd\u8dd1\u51fa\u6b63\u78ba\u7b54\u6848\uff0c\u4f46\u662f\u5728 Leetcode \u4e0a\u8dd1\u4e0d\u51fa\u4f86\uff0c\u6211\u7adf\u7136\u70ba\u9019\u7a2e\u554f\u984c\u6d6a\u8cbb\u4e86\u4e0d\u5c11\u6642\u9593\uff0c\u6700\u7d42\u4e5f\u6c92\u89e3\u6c7a\u2026\u2026 \u6b63\u78ba\u7cbe\u7df4\u7684 Switch \u89e3\u6cd5\uff1a 6 ms \u904b\u7528 Hashmap\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,18,135],"tags":[137],"class_list":["post-1957","post","type-post","status-publish","format-standard","hentry","category-all","category-18","category-135","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1957"}],"version-history":[{"count":2,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1957\/revisions"}],"predecessor-version":[{"id":1959,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=\/wp\/v2\/posts\/1957\/revisions\/1959"}],"wp:attachment":[{"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zttofficial.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}