{"id":1881,"date":"2016-03-17T21:37:04","date_gmt":"2016-03-17T13:37:04","guid":{"rendered":"http:\/\/boweihe.me\/?p=1881"},"modified":"2016-03-17T21:37:04","modified_gmt":"2016-03-17T13:37:04","slug":"leetcode-70-climbing-stairs","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1881","title":{"rendered":"LeetCode 70. Climbing Stairs"},"content":{"rendered":"<p><strong>\u9898\u76ee:<\/strong>\u00a0https:\/\/leetcode.com\/problems\/climbing-stairs\/<br \/>\n<strong>\u601d\u8def\uff1a<\/strong><br \/>\n\u9012\u5f52\u9012\u5f52\uff0c\u4f46\u662f\u8ddf\u6590\u6ce2\u90a3\u5951\u6570\u90a3\u4e2a\u4e00\u6837\uff0c\u8981\u4fdd\u5b58\u4e00\u4e9b\u4e2d\u95f4\u7b97\u51fa\u6765\u7684\u7ed3\u679c\u5907\u7528\uff0c\u4e0d\u7136\u4f1a\u8d85\u65f6\uff01<br \/>\n<strong>\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    map&lt;int, int&gt; resultMap;\n    int climbStairs(int n) {\n        if(n == 2){\n            \/\/ 2 or 1+1, two ways\n            return 2;\n        } else if(n == 1) {\n            return 1;\n        } else if (n == 0) {\n            return 0;\n        } else {\n            int c_n1, c_n2;\n            if(resultMap.count(n-1) &gt; 0)\n                c_n1 = resultMap[n-1];\n            else {\n                c_n1 = climbStairs(n-1);\n                resultMap[n-1] = c_n1;\n            }\n            if(resultMap.count(n-2) &gt; 0)\n                c_n2 = resultMap[n-2];\n            else {\n                c_n2 = climbStairs(n-2);\n                resultMap[n-2] = c_n2;\n            }\n            return c_n1 + c_n2;\n        }\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee:\u00a0https:\/\/leetcode.com\/problems\/climbing-stairs\/ \u601d\u8def\uff1a \u9012\u5f52\u9012\u5f52\uff0c\u4f46\u662f\u8ddf\u6590\u6ce2\u90a3\u5951\u6570\u90a3\u4e2a\u4e00\u6837\uff0c\u8981\u4fdd\u5b58\u4e00\u4e9b\u4e2d\u95f4\u7b97\u51fa\u6765\u7684\u7ed3\u679c\u5907\u7528\uff0c\u4e0d\u7136\u4f1a\u8d85\u65f6\uff01 \u4ee3\u7801\uff1a class Solution { public: map&lt;int, int&gt; resultMap; int climbStairs(int n) { if(n == 2){ \/\/ 2 or 1+1, two ways return 2; } else if(n == 1) { return 1; } else if (n == 0) { return 0; } else { int c_n1, c_n2; if(resultMap.count(n-1) &gt; 0) c_n1 = resultMap[n-1]; [&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-1881","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\/1881","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=1881"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1881\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}