
{"id":914,"date":"2020-07-15T20:05:15","date_gmt":"2020-07-15T12:05:15","guid":{"rendered":"https:\/\/www.changxuan.top\/?p=914"},"modified":"2020-10-12T20:05:05","modified_gmt":"2020-10-12T12:05:05","slug":"climbing-stairs","status":"publish","type":"post","link":"https:\/\/www.changxuan.top\/?p=914","title":{"rendered":"Climbing Stairs"},"content":{"rendered":"\n<p>You are climbing a stair case. It takes&nbsp;<em>n<\/em>&nbsp;steps to reach to the top.<\/p>\n\n\n\n<p>Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are two ways to climb to the top.\n1. 1 step + 1 step\n2. 2 steps\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> There are three ways to climb to the top.\n1. 1 step + 1 step + 1 step\n2. 1 step + 2 steps\n3. 2 steps + 1 step\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 45<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution<\/strong>:<\/p>\n\n\n<p><!-- wp:syntaxhighlighter\/code \n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">class Solution {\n    public int climbStairs(int n) {\n       if(n == 0){\n            return 0;\n        }else if(n ==1){\n            return 1;\n        }else if(n == 2){\n            return 2;\n        }\n        int[] dp = new int[n+1];\n        dp[1] = 1;\n        dp[2] = 2;\n        for (int i = 3; i &lt;= n; i++){\n            dp[i] = dp[i-1]+dp[i-2];\n        }\n        return dp[n]; \n    }\n}<\/pre>\n\n\n \/wp:syntaxhighlighter\/code --><\/p>\n<section id=\"nice\" data-tool=\"mdnice\u7f16\u8f91\u5668\" data-website=\"https:\/\/www.mdnice.com\" style=\"font-size: 16px; color: black; padding: 0 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;\">\n<pre class=\"custom\" data-tool=\"mdnice\u7f16\u8f91\u5668\" style=\"margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;\"><span style=\"display: block; background: url(https:\/\/my-wechat.mdnice.com\/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;\"><\/span><code class=\"hljs\" style=\"overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;\"><span class=\"hljs-function\" style=\"line-height: 26px;\"><span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">public<\/span>&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">int<\/span>&nbsp;<span class=\"hljs-title\" style=\"color: #61aeee; line-height: 26px;\">climbStairs<\/span><span class=\"hljs-params\" style=\"line-height: 26px;\">(<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">int<\/span>&nbsp;n)<\/span>&nbsp;<\/span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">if<\/span>(n&nbsp;==&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">0<\/span>){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">return<\/span>&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">0<\/span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">else<\/span>&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">if<\/span>(n&nbsp;==<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">1<\/span>){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">return<\/span>&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">1<\/span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">else<\/span>&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">if<\/span>(n&nbsp;==&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">2<\/span>){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">return<\/span>&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">2<\/span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">int<\/span>[]&nbsp;dp&nbsp;=&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">new<\/span>&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">int<\/span>[n+<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">1<\/span>];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">1<\/span>]&nbsp;=&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">1<\/span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">2<\/span>]&nbsp;=&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">2<\/span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">for<\/span>&nbsp;(<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">int<\/span>&nbsp;i&nbsp;=&nbsp;<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">3<\/span>;&nbsp;i&nbsp;&amp;lt;=&nbsp;n;&nbsp;i++){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dp[i]&nbsp;=&nbsp;dp[i-<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">1<\/span>]+dp[i-<span class=\"hljs-number\" style=\"color: #d19a66; line-height: 26px;\">2<\/span>];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"hljs-keyword\" style=\"color: #c678dd; line-height: 26px;\">return<\/span>&nbsp;dp[n];&nbsp;<br>}<\/code><\/pre>\n<\/section>","protected":false},"excerpt":{"rendered":"<p>You are climbing a stair case. It takes&nbsp;n&nbsp;ste &hellip; <a href=\"https:\/\/www.changxuan.top\/?p=914\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Climbing Stairs<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[82],"class_list":["post-914","post","type-post","status-publish","format-standard","hentry","category-tech","tag-dynamic-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Climbing Stairs - \u5e38\u8f69<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.changxuan.top\/?p=914\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Climbing Stairs - \u5e38\u8f69\" \/>\n<meta property=\"og:description\" content=\"You are climbing a stair case. It takes&nbsp;n&nbsp;ste &hellip; \u7ee7\u7eed\u9605\u8bfbClimbing Stairs\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.changxuan.top\/?p=914\" \/>\n<meta property=\"og:site_name\" content=\"\u5e38\u8f69\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-15T12:05:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-10-12T12:05:05+00:00\" \/>\n<meta name=\"author\" content=\"\u5e38\u8f69\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u5e38\u8f69\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=914#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=914\"},\"author\":{\"name\":\"\u5e38\u8f69\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"headline\":\"Climbing Stairs\",\"datePublished\":\"2020-07-15T12:05:15+00:00\",\"dateModified\":\"2020-10-12T12:05:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=914\"},\"wordCount\":42,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"keywords\":[\"Dynamic Programming\"],\"articleSection\":[\"\u6280\u672f\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.changxuan.top\\\/?p=914#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=914\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/?p=914\",\"name\":\"Climbing Stairs - \u5e38\u8f69\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#website\"},\"datePublished\":\"2020-07-15T12:05:15+00:00\",\"dateModified\":\"2020-10-12T12:05:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=914#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.changxuan.top\\\/?p=914\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=914#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/www.changxuan.top\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Climbing Stairs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#website\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/\",\"name\":\"\u5e38\u8f69\",\"description\":\"\u95fb\u9053\u6709\u5148\u540e\uff0c\u672f\u4e1a\u6709\u4e13\u653b-\u4e00\u4e8c\u4e09\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.changxuan.top\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\",\"name\":\"\u5e38\u8f69\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"contentUrl\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"width\":960,\"height\":960,\"caption\":\"\u5e38\u8f69\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\"},\"description\":\"\u603b\u8981\u505a\u70b9\u4ec0\u4e48\u5427\uff01\",\"sameAs\":[\"https:\\\/\\\/www.changxuan.top\"],\"url\":\"https:\\\/\\\/www.changxuan.top\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Climbing Stairs - \u5e38\u8f69","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.changxuan.top\/?p=914","og_locale":"zh_CN","og_type":"article","og_title":"Climbing Stairs - \u5e38\u8f69","og_description":"You are climbing a stair case. It takes&nbsp;n&nbsp;ste &hellip; \u7ee7\u7eed\u9605\u8bfbClimbing Stairs","og_url":"https:\/\/www.changxuan.top\/?p=914","og_site_name":"\u5e38\u8f69","article_published_time":"2020-07-15T12:05:15+00:00","article_modified_time":"2020-10-12T12:05:05+00:00","author":"\u5e38\u8f69","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u5e38\u8f69","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.changxuan.top\/?p=914#article","isPartOf":{"@id":"https:\/\/www.changxuan.top\/?p=914"},"author":{"name":"\u5e38\u8f69","@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"headline":"Climbing Stairs","datePublished":"2020-07-15T12:05:15+00:00","dateModified":"2020-10-12T12:05:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.changxuan.top\/?p=914"},"wordCount":42,"commentCount":0,"publisher":{"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"keywords":["Dynamic Programming"],"articleSection":["\u6280\u672f"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.changxuan.top\/?p=914#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.changxuan.top\/?p=914","url":"https:\/\/www.changxuan.top\/?p=914","name":"Climbing Stairs - \u5e38\u8f69","isPartOf":{"@id":"https:\/\/www.changxuan.top\/#website"},"datePublished":"2020-07-15T12:05:15+00:00","dateModified":"2020-10-12T12:05:05+00:00","breadcrumb":{"@id":"https:\/\/www.changxuan.top\/?p=914#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.changxuan.top\/?p=914"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.changxuan.top\/?p=914#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.changxuan.top\/"},{"@type":"ListItem","position":2,"name":"Climbing Stairs"}]},{"@type":"WebSite","@id":"https:\/\/www.changxuan.top\/#website","url":"https:\/\/www.changxuan.top\/","name":"\u5e38\u8f69","description":"\u95fb\u9053\u6709\u5148\u540e\uff0c\u672f\u4e1a\u6709\u4e13\u653b-\u4e00\u4e8c\u4e09","publisher":{"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.changxuan.top\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd","name":"\u5e38\u8f69","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","url":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","contentUrl":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","width":960,"height":960,"caption":"\u5e38\u8f69"},"logo":{"@id":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg"},"description":"\u603b\u8981\u505a\u70b9\u4ec0\u4e48\u5427\uff01","sameAs":["https:\/\/www.changxuan.top"],"url":"https:\/\/www.changxuan.top\/?author=1"}]}},"views":1246,"_links":{"self":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=914"}],"version-history":[{"count":7,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/914\/revisions"}],"predecessor-version":[{"id":994,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/914\/revisions\/994"}],"wp:attachment":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}