{"id":2184,"date":"2016-09-10T16:49:08","date_gmt":"2016-09-10T08:49:08","guid":{"rendered":"http:\/\/boweihe.me\/?p=2184"},"modified":"2016-09-10T16:49:08","modified_gmt":"2016-09-10T08:49:08","slug":"leetcode-91-decode-ways","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2184","title":{"rendered":"LeetCode 91. Decode Ways"},"content":{"rendered":"<h3>\u601d\u8def<\/h3>\n<p>\u8b66\u544a\uff1a\u5343\u4e07\u6ce8\u610f&#8217;0&#8217;\u8fd9\u4e2a\u5f02\u7c7b\uff0c\u5f53\u7136\u4e86\u8fd8\u6709\u7a7a\u8f93\u5165:(\u3002<br \/>\n\u5176\u5b9e\u4e5f\u662f\u4e2a\u52a8\u6001\u89c4\u5212?\u7684\u95ee\u9898\uff0c\u6709\u70b9\u50cf\u90a3\u4e2a\u8d70\u697c\u68af\u7684\u9898<br \/>\n\u5f53\u524d\u80fddecode\u7684\u65b9\u6cd5\u6570\u4e0e\u4e4b\u524d\u7684\u4e24\u79cd\u72b6\u6001\u6709\u5173\uff0c\u5c31\u662f<\/p>\n<ul>\n<li>\u5224\u65ad\u5f53\u524d\u7684\u5b57\u6bcds[i]\u80fd\u4e0d\u80fd\u5355\u4e2adecode\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u90a3\u65b9\u6cd5\u6570\u5c31\u8981\u52a0\u4e0a\u89e3\u7801s[i-1]\u65f6\u7684\u65b9\u6cd5\u6570\uff0c\u5982\u679c\u4e0d\u80fd\u89e3\u90a3\u5c31\u4e0d\u52a0\uff1b<\/li>\n<li>\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26s[i-1]s[i]\u7684\u7ec4\u5408\u80fd\u4e0d\u80fd\u89e3\u7801\uff08\u5c31\u662f\u5728\u4e0d\u572810-26\u7684\u8303\u56f4\u5185\uff09\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u62ff\u52a0\u4e0a\u89e3\u7801s[i-2]\u65f6\u7684\u65b9\u6cd5\u6570\uff1b<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u4e8b\u5b9e\u4e0a\u53ea\u8981\u4fdd\u5b58s[i],s[i-1]\u4e24\u4e2a\u65b9\u6cd5\u6570\u7684\u7ed3\u679c\u5c31\u884c\uff0c\u6211\u7684\u4ee3\u7801\u7a7a\u95f4\u590d\u6742\u5ea6\u8fd8\u6bd4\u8f83\u5927\uff0c\u4f46\u662f\u4e0d\u60f3\u6539\u4e86\u3002<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    bool isLegalBiChar(char m, char n){\n        \/\/ is \"mn\" a legal word?\n        if(m != '1' &amp;&amp; m != '2')\n            return false;\n        if(m == '2'){\n            if(n &gt; '6')\n                return false;\n        }\n        return true;\n    }\n    int numDecodings(string s) {\n        if(s.empty() || s[0]=='0')\n            return 0;\n        int* decodeWays = new int[s.size()];\n        \/\/be care of '0's\n        decodeWays[0] = 1;\n        for(int i=1; i&lt;s.size(); i++){\n            int ways = 0;\n            if(s[i] &gt; '0' &amp;&amp; s[i] &lt;= '9'){\n                ways += decodeWays[i-1];\n            }\n            if(isLegalBiChar(s[i-1], s[i])){\n                if(i&lt;2)\n                    ways += 1;\n                else\n                    ways += decodeWays[i-2];\n            }\n            decodeWays[i] = ways;\n        }\n        return decodeWays[s.size()-1];\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def \u8b66\u544a\uff1a\u5343\u4e07\u6ce8\u610f&#8217;0&#8217;\u8fd9\u4e2a\u5f02\u7c7b\uff0c\u5f53\u7136\u4e86\u8fd8\u6709\u7a7a\u8f93\u5165:(\u3002 \u5176\u5b9e\u4e5f\u662f\u4e2a\u52a8\u6001\u89c4\u5212?\u7684\u95ee\u9898\uff0c\u6709\u70b9\u50cf\u90a3\u4e2a\u8d70\u697c\u68af\u7684\u9898 \u5f53\u524d\u80fddecode\u7684\u65b9\u6cd5\u6570\u4e0e\u4e4b\u524d\u7684\u4e24\u79cd\u72b6\u6001\u6709\u5173\uff0c\u5c31\u662f \u5224\u65ad\u5f53\u524d\u7684\u5b57\u6bcds[i]\u80fd\u4e0d\u80fd\u5355\u4e2adecode\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u90a3\u65b9\u6cd5\u6570\u5c31\u8981\u52a0\u4e0a\u89e3\u7801s[i-1]\u65f6\u7684\u65b9\u6cd5\u6570\uff0c\u5982\u679c\u4e0d\u80fd\u89e3\u90a3\u5c31\u4e0d\u52a0\uff1b \u5224\u65ad\u4e24\u4e2a\u5b57\u7b26s[i-1]s[i]\u7684\u7ec4\u5408\u80fd\u4e0d\u80fd\u89e3\u7801\uff08\u5c31\u662f\u5728\u4e0d\u572810-26\u7684\u8303\u56f4\u5185\uff09\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u62ff\u52a0\u4e0a\u89e3\u7801s[i-2]\u65f6\u7684\u65b9\u6cd5\u6570\uff1b \u6240\u4ee5\u4e8b\u5b9e\u4e0a\u53ea\u8981\u4fdd\u5b58s[i],s[i-1]\u4e24\u4e2a\u65b9\u6cd5\u6570\u7684\u7ed3\u679c\u5c31\u884c\uff0c\u6211\u7684\u4ee3\u7801\u7a7a\u95f4\u590d\u6742\u5ea6\u8fd8\u6bd4\u8f83\u5927\uff0c\u4f46\u662f\u4e0d\u60f3\u6539\u4e86\u3002 \u4ee3\u7801 class Solution { public: bool isLegalBiChar(char m, char n){ \/\/ is &#8220;mn&#8221; a legal word? if(m != &#8216;1&#8217; &amp;&amp; m != &#8216;2&#8217;) return false; if(m == &#8216;2&#8217;){ if(n &gt; &#8216;6&#8217;) return false; } return true; } int numDecodings(string s) { if(s.empty() || s[0]==&#8217;0&#8242;) return 0; int* decodeWays [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,141,151],"class_list":["post-2184","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-141","tag-151"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2184"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2184\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}