{"id":1860,"date":"2016-03-06T16:13:23","date_gmt":"2016-03-06T08:13:23","guid":{"rendered":"http:\/\/boweihe.me\/?p=1860"},"modified":"2016-03-06T16:13:23","modified_gmt":"2016-03-06T08:13:23","slug":"leetcode-17-letter-combinations-of-a-phone-number","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1860","title":{"rendered":"LeetCode 17. Letter Combinations of a Phone Number"},"content":{"rendered":"<p><strong>\u539f\u9898\uff1a<\/strong>https:\/\/leetcode.com\/problems\/letter-combinations-of-a-phone-number\/<br \/>\n<strong>\u601d\u8def\uff1a<\/strong><br \/>\n\u5c31\u662f\u6392\u5217\u7ec4\u5408\u5168\u90e8\u8f93\u51fa\u561b\uff01\u5982\u679c\u7528\u9012\u5f52\u505a\u7684\u8bdd\uff0c\u51fd\u6570\u8fdb\u6808\u51fa\u6808\u4f30\u8ba1\u4f1a\u633a\u8d39\u4e8b\u513f\u7684\uff0c\u6240\u4ee5\u60f3\u4e2a\u5faa\u73af\u5457\u3002\u5047\u8bbe\u6211\u4eec\u8981\u8f93\u51fa\u7684\u662f\u4e00\u4e2a\u77e9\u9635\u5f62\u5f0f\u7684\u4e1c\u897f\uff0c\u7b2ci\u884cj\u5217\u662f\u7b2ci\u4e2a\u7b54\u6848\u7684\u7b2cj\u4e2a\u5b57\u5e55\uff0c\u90a3\u4e48\u4e3b\u8981\u95ee\u9898\u662f\u6c42\u51fa\u8fd9\u4e2a\u5b57\u6bcd\u662f\u5565\u3002<br \/>\n\u62ff\u539f\u9898\u4e2d\u7ed9\u7684\u4f8b\u5b50\u770b\uff0c\u5bf9\u5e94\u7b2c\u4e00\u5217\u7684\u8f93\u51fa\u662fa,a,a,b,b,b,c,c,c\uff1b\u7b2c\u4e8c\u5217\u662fd,e,f,d,e,f,d,e,f. \u53ef\u4ee5\u6bd4\u8f83\u5bb9\u6613\u77e5\u9053\uff0c\u7b2c\u4e00\u5217\u7684\u5b57\u6bcd\u662f\u6bcf3\u5217\u53d8\u6362\u4e00\u6b21\uff0c\u7b2c\u4e8c\u5217\u5219\u662f\u6bcf1\u5217\u53d8\u6362\u4e00\u6b21\u3002\u5373\uff0c\u5047\u8bbe\u6700\u7ec8\u7531N\u4e2a\u53ef\u80fd\u7ed3\u679c\uff08N\u884c\uff09\uff0c\u6bcf\u4e00\u5217\u4ee3\u8868\u6309\u4e86\u4e00\u4e2a\u6570\u5b57\u952e\uff0c\u5176\u5bf9\u5e94\u5b57\u6bcd\u6709m[j]\u4e2a\u90a3\u4e48\uff1a<\/p>\n<ol>\n<li>\u7b2c1\u5217(j=0)\u5b57\u6bcd\u662f\u6bcfY[0] = N\/m[j]\u6539\u53d8\u4e00\u6b21\u7684\uff1b<\/li>\n<li>\u7b2c2\u5217(j=1)\u5b57\u6bcd\u662f\u6bcfY[1]\/m[j]\u6539\u53d8\u4e00\u6b21\u7684\uff1b<\/li>\n<li>\u4ee5\u6b64\u7c7b\u63a8&#8230;<\/li>\n<\/ol>\n<p>\u6240\u4ee5\u7b2ci\u884c\u7b2cj\u5217\u7684\u5b57\u7b26\u5e94\u8be5\u662fchr[ dight[i] ] [ (j\/Y[j])%\u5f53\u524d\u6570\u5b57\u5bf9\u5e94\u53ef\u80fd\u7ed3\u679c\u7684\u4e2a\u6570 ]<br \/>\n\u5176\u4e2dj\/Y[j]\u662f\u5f53\u524d\u8df3\u5230\u4e86\u54ea\u4e00\u4e2a\u201c\u8f6e\u56de\u201d\u4e86\uff0c\u6bd4\u5982\u4f8b\u5b50\u91cc\u9762j=4,Y[0]=9\/3=3\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u8dd1\u5230\u7b2c\u4e8c\u4e2a\u5b57\u6bcd&#8221;b&#8221;\u4e86\u3002<br \/>\n<strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">struct ChrNode{\n    int size;\n    ChrNode() {}\n};\nconst char chrs[][4] = { {' '}, {'X'}, {'a','b','c'}, {'d','e','f'},\n                         {'g','h','i'}, {'j','k','l'}, {'m','n','o'},\n                         {'p','q','r','s'}, {'t','u','v'}, {'w','x','y','z'}};\nclass Solution {\npublic:\n    int childSize(char c) {\n        switch (c) {\n            case '2': case '3': case '4': case '5': case '6': case '8': return 3;\n            case '7': case '9': return 4;\n        }\n        return 1;\n    }\n    void makeNode(ChrNode* node, char d) {\n        node-&gt;size = childSize(d);\n    }\n    vector&lt;string&gt; letterCombinations(string digits) {\n        vector&lt;ChrNode&gt; nodes;\n        \/\/Build tree\n        int digiSize = digits.size();\n        int solutionSz = 1;\n        for(int i=0; i&lt;digiSize; i++){\n            ChrNode node;\n            makeNode(&amp;node, digits[i]);\n            nodes.push_back(node);\n            solutionSz *= childSize(digits[i]);\n        }\n        vector&lt;string&gt; solutions;\n        if(solutionSz &lt;= 1)\n            return solutions;\n        for(int row=0; row&lt;solutionSz; row++)\n            solutions.push_back(\"\");\n        char c;\n        int jmp = solutionSz;\n        for(int i=0; i&lt;digiSize; i++) {\n            jmp \/= nodes[i].size;\n            int jmp_diff;\n            for(int j=0; j&lt;solutionSz; j++) {\n                jmp_diff = j \/ jmp;\n                c = chrs[digits[i] - '0'][jmp_diff % nodes[i].size];\n                solutions[j] += c;\n            }\n        }\n        return solutions;\n    }\n};\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u9898\uff1ahttps:\/\/leetcode.com\/problems\/letter-combinations-of-a-phone-number\/ \u601d\u8def\uff1a \u5c31\u662f\u6392\u5217\u7ec4\u5408\u5168\u90e8\u8f93\u51fa\u561b\uff01\u5982\u679c\u7528\u9012\u5f52\u505a\u7684\u8bdd\uff0c\u51fd\u6570\u8fdb\u6808\u51fa\u6808\u4f30\u8ba1\u4f1a\u633a\u8d39\u4e8b\u513f\u7684\uff0c\u6240\u4ee5\u60f3\u4e2a\u5faa\u73af\u5457\u3002\u5047\u8bbe\u6211\u4eec\u8981\u8f93\u51fa\u7684\u662f\u4e00\u4e2a\u77e9\u9635\u5f62\u5f0f\u7684\u4e1c\u897f\uff0c\u7b2ci\u884cj\u5217\u662f\u7b2ci\u4e2a\u7b54\u6848\u7684\u7b2cj\u4e2a\u5b57\u5e55\uff0c\u90a3\u4e48\u4e3b\u8981\u95ee\u9898\u662f\u6c42\u51fa\u8fd9\u4e2a\u5b57\u6bcd\u662f\u5565\u3002 \u62ff\u539f\u9898\u4e2d\u7ed9\u7684\u4f8b\u5b50\u770b\uff0c\u5bf9\u5e94\u7b2c\u4e00\u5217\u7684\u8f93\u51fa\u662fa,a,a,b,b,b,c,c,c\uff1b\u7b2c\u4e8c\u5217\u662fd,e,f,d,e,f,d,e,f. \u53ef\u4ee5\u6bd4\u8f83\u5bb9\u6613\u77e5\u9053\uff0c\u7b2c\u4e00\u5217\u7684\u5b57\u6bcd\u662f\u6bcf3\u5217\u53d8\u6362\u4e00\u6b21\uff0c\u7b2c\u4e8c\u5217\u5219\u662f\u6bcf1\u5217\u53d8\u6362\u4e00\u6b21\u3002\u5373\uff0c\u5047\u8bbe\u6700\u7ec8\u7531N\u4e2a\u53ef\u80fd\u7ed3\u679c\uff08N\u884c\uff09\uff0c\u6bcf\u4e00\u5217\u4ee3\u8868\u6309\u4e86\u4e00\u4e2a\u6570\u5b57\u952e\uff0c\u5176\u5bf9\u5e94\u5b57\u6bcd\u6709m[j]\u4e2a\u90a3\u4e48\uff1a \u7b2c1\u5217(j=0)\u5b57\u6bcd\u662f\u6bcfY[0] = N\/m[j]\u6539\u53d8\u4e00\u6b21\u7684\uff1b \u7b2c2\u5217(j=1)\u5b57\u6bcd\u662f\u6bcfY[1]\/m[j]\u6539\u53d8\u4e00\u6b21\u7684\uff1b \u4ee5\u6b64\u7c7b\u63a8&#8230; \u6240\u4ee5\u7b2ci\u884c\u7b2cj\u5217\u7684\u5b57\u7b26\u5e94\u8be5\u662fchr[ dight[i] ] [ (j\/Y[j])%\u5f53\u524d\u6570\u5b57\u5bf9\u5e94\u53ef\u80fd\u7ed3\u679c\u7684\u4e2a\u6570 ] \u5176\u4e2dj\/Y[j]\u662f\u5f53\u524d\u8df3\u5230\u4e86\u54ea\u4e00\u4e2a\u201c\u8f6e\u56de\u201d\u4e86\uff0c\u6bd4\u5982\u4f8b\u5b50\u91cc\u9762j=4,Y[0]=9\/3=3\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u8dd1\u5230\u7b2c\u4e8c\u4e2a\u5b57\u6bcd&#8221;b&#8221;\u4e86\u3002 \u4ee3\u7801\uff1a struct ChrNode{ int size; ChrNode() {} }; const char chrs[][4] = { {&#8216; &#8216;}, {&#8216;X&#8217;}, {&#8216;a&#8217;,&#8217;b&#8217;,&#8217;c&#8217;}, {&#8216;d&#8217;,&#8217;e&#8217;,&#8217;f&#8217;}, {&#8216;g&#8217;,&#8217;h&#8217;,&#8217;i&#8217;}, {&#8216;j&#8217;,&#8217;k&#8217;,&#8217;l&#8217;}, {&#8216;m&#8217;,&#8217;n&#8217;,&#8217;o&#8217;}, {&#8216;p&#8217;,&#8217;q&#8217;,&#8217;r&#8217;,&#8217;s&#8217;}, {&#8216;t&#8217;,&#8217;u&#8217;,&#8217;v&#8217;}, {&#8216;w&#8217;,&#8217;x&#8217;,&#8217;y&#8217;,&#8217;z&#8217;}}; class Solution { public: int childSize(char c) { switch (c) { case &#8216;2&#8217;: case [&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],"class_list":["post-1860","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1860","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=1860"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1860\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}