当前位置:网站首页>Leetcode (Sword finger offer) - 10- ii Frog jumping on steps

Leetcode (Sword finger offer) - 10- ii Frog jumping on steps

2022-06-11 10:50:00 Programmer code

Topic link : Click to open the link

The main idea of the topic : A little .

Their thinking : A little .

Related enterprises

  • Bytes to beat
  • Microsoft (Microsoft)
  • Google (Google)
  • Bloomberg (Bloomberg)
  • tencent (Tencent)
  • Apple (Apple)
  • Goldman Sachs Group (Goldman Sachs)
  • Facebook
  • Amazon (Amazon)

AC Code

  • Java
//  Solution (1)
class Solution {
    public int numWays(int n) {
        if (n == 0) return 1;
        int[] dp = new int[n + 1];
        dp[0] = dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007;
        }
 
        return dp[n];
    }
}

//  Solution (2)
class Solution {
    public int numWays(int n) {
        int a = 1, b = 1, sum;
        for(int i = 0; i < n; i++){
            sum = (a + b) % 1000000007;
            a = b;
            b = sum;
        }
        return a;
    }
}
  • C++
class Solution {
public:
    int numWays(int n) {
        int a = 1, b = 1, sum;
        for(int i = 0; i < n; i++){
            sum = (a + b) % 1000000007;
            a = b;
            b = sum;
        }
        return a;
    }
};
原网站

版权声明
本文为[Programmer code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012230181476.html