当前位置:网站首页>[leetcode sword finger offer 10 - II. Frog jumping steps (simple)]

[leetcode sword finger offer 10 - II. Frog jumping steps (simple)]

2022-04-23 21:21:00 Minaldo7

subject :

A frog can jump up at a time 1 Stepped steps , You can jump on it 2 Stepped steps . Ask the frog to jump on one n How many jumps are there in the steps .

The answer needs to be modelled 1e9+7(1000000007), If the initial result of calculation is :1000000008, Please return 1.

Example 1:

Input :n = 2
Output :2
Example 2:

Input :n = 7
Output :21
Example 3:

Input :n = 0
Output :1
Tips :

0 <= n <= 100

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

The problem solving process :

class Solution {
    
    public int numWays(int n) {
    
	
        // if(n == 0 || n == 1) return 1;
        // int front = 1, back = 1, fibn =0;
        // for(int i = 2;i<=n;i++){
    
        // fibn = front + back;
        // if(fibn >= 1000000007)
        // fibn -= 1000000007;
        // front = back;
        // back = fibn;
        // }
        // return fibn;

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

Execution results :

 Insert picture description here

notes : Multiplication of large numbers , Why should we take modules for the permutation and combination of large numbers ?
because :1000000007 It's a prime number ( prime number ), The remainder of prime numbers can avoid the conflict of results to the greatest extent / repeat

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