​LeetCode刷題實戰70:爬樓梯

2021-03-02 程序IT圈

算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !

今天和大家聊的問題叫做 爬樓梯,我們先來看題面:

https://leetcode-cn.com/problems/climbing-stairs/

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

題意每次你可以爬 1 或 2 個臺階。你有多少種不同的方法可以爬到樓頂呢?

示例 1:

輸入:2
輸出:2
解釋:有兩種方法可以爬到樓頂。
1. 1 階 + 1 階
2. 2 階

示例 2:

輸入:3
輸出:3
解釋:有三種方法可以爬到樓頂。
1. 1 階 + 1 階 + 1 階
2. 1 階 + 2 階
3. 2 階 + 1 階

解題


思路:可以這樣想,n個臺階,一開始可以爬 1 步,也可以爬 2 步,那麼n個臺階爬樓的爬樓方法就等於 一開始爬1步的方法數 + 一開始爬2步的方法數,這樣我們就只需要計算n-1個臺階的方法數和n-2個臺階方法數,同理,計算n-1個臺階的方法數只需要計算一下n-2個臺階和n-3個臺階,計算n-2個臺階需要計算一下n-3個臺階和n-4個臺階……


class Solution {
    public int climbStairs(int n) {
        if(n==1)return 1;
        int sum[]=new int[n+1];
        sum[0]=0;sum[1]=1;sum[2]=2;
     for(int i=3;i<=n;i++){
         sum[i]=sum[i-2]+sum[i-1];
     }
        return sum[n];
    }
}


好了,今天的文章就到這裡,如果覺得有所收穫,請順手點個在看或者轉發吧,你們的支持是我最大的動力。

相關焦點

  • LeetCode 70 爬樓梯
    1)動態規劃‍‍‍‍‍本問題其實常規解法可以分成多個子問題,爬第n階樓梯的方法數量,等於2部分之和1.爬上n- 1階樓梯的方法數量。因為再爬1階就能到第n階2.爬上n - 2階樓梯的方法數量,因為再爬2階就能到第n階所以我們得到公式 dp[n] = dp[n - 1] + dp[n - 2]同時需要初始化dp[0]= 1和dp[1]= 1‍2)代碼呈現class Solution
  • leetcode 每日一題:746. 使用最小花費爬樓梯
    一起刷題吧一、題目分析 輸入:由數值組成的數組輸出:到達樓層頂部的最低花費難度:簡單標籤:貪心,動態規劃示例 1:輸入: cost = [10, 15, 20二、參考代碼 這個題是動態規劃裡比較簡單的題目,動態方程也比較好想:F[i] 表示走到第 i 層需要的最小的步數,只是走到F[i] = min(F[i-1] + cost[i-1], F[i-2] + cost[i-2])最終結果是 F[len(cost)] 即走過第 len(cost)-1 層參考代碼如下:from typing
  • 【LeetCode之C#解法】 移動零、爬樓梯
    https://leetco} } }https://leetcoleetcode-cn.com/problems/climbing-stairs/70. 爬爬樓梯假設你正在爬樓梯。需要 n 階你才能到達樓頂。每次你可以爬 1 或 2個臺階。你有多少種不同的方法可以爬到樓頂呢?
  • ​LeetCode刷題實戰139:單詞拆分
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 單詞拆分,我們先來看題面:https://leetcode-cn.com/problems/word-break/Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be
  • 帶你狂刷算法Leetcode題!短時間內快速獲得實戰能力!
    但對於初學者來說,很容易沉迷在刷題的數量中,覺得如果能刷完這1000道題,自己一定能夠有所飛躍。但實際上,低效率的重複對你來說,根本就無法掌握到解題的精髓,一旦題目有所變動,就無法舉一反三。那究竟應該怎麼刷題才高效呢?
  • ​LeetCode刷題實戰79:單詞搜索
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 單詞搜索,我們先來看題面:https://leetcode-cn.com/problems/word-search/Given a 2D board and a word, find if the word exists in the grid.
  • 動態規劃刷題篇(1) 斐波那契、爬樓梯、路徑與最長遞增子序列
    https://leetcode-cn.com/problems/fibonacci-number這題作為動態規劃的入門題實在是再合適不過了,斐波那契數列其實每個同學在小學就接觸過,可能厲害的同學口算都會(這就是小學二年級的知識嗎)
  • ​LeetCode刷題實戰36: 有效的數獨
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 有效的數獨,我們先來看題面:https://leetcode-cn.com/problems/valid-sudoku/Determine if a 9x9 Sudoku board is valid.
  • ​LeetCode刷題實戰198:打家劫舍
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 打家劫舍,我們先來看題面:https://leetcode-cn.com/problems/house-robber/You are a professional robber planning to rob houses along a street.
  • ​LeetCode刷題實戰42:接雨水
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 接雨水,我們先來看題面:https://leetcode-cn.com/problems/trapping-rain-water/Given n non-negative integers representing an elevation map where the width of each bar
  • ​LeetCode刷題實戰57:插入區間
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 插入區間,我們先來看題面:https://leetcode-cn.com/problems/insert-interval/Given a set of non-overlapping intervals, insert a new interval into the intervals (merge
  • ​LeetCode刷題實戰179:最大數
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 最大數  ,我們先來看題面:https://leetcode-cn.com/problems/largest-number/Given a list of non-negative integers nums, arrange them such that they form the largest number.
  • ​LeetCode刷題實戰134:加油站
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !https://leetcode-cn.com/problems/gas-station/There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
  • 春節大禮包|刷題技巧+80道Leetcode
    為了跳槽,我前兩年的春節都是在刷題中度過的,目前為止刷了小四百道leetcode,也算是有一些經驗,今天就跟大家分享下學習方法和我總結的乾貨。後來發現了 Leetbook[1] 這個寶藏,才算是找到了適合自己的刷題方法。
  • ​LeetCode刷題實戰133:克隆圖
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !https://leetcode-cn.com/problems/clone-graph/Given a reference of a node in a connected undirected graph.
  • ​LeetCode刷題實戰39:組合總和
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 組合總和,我們先來看題面:https://leetcode-cn.com/problems/combination-sumGiven a set of candidate numbers (candidates) (without duplicates) and a target number (target
  • ​LeetCode刷題實戰72:編輯距離
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 編輯距離,我們先來看題面:https://leetcode-cn.com/problems/edit-distanceGiven two words word1 and word2, find the minimum number of operations required to convert word1
  • leetcode刷題最強指南(版本1.0)
    為什麼會有這篇刷題指南很多剛開始刷題的同學都有一個困惑:面對leetcode上近兩千道題目,從何刷起
  • ​LeetCode刷題實戰119: 楊輝三角 II
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 楊輝三角 II,我們先來看題面:https://leetcode-cn.com/problems/pascals-triangle-ii/Given an integer rowIndex, return the rowIndexth row of the Pascal's triangle.
  • ​LeetCode刷題實戰18: 四數之和
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做四數之和 ,我們先來看題面:https://leetcode-cn.com/problems/4sum/Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such