내공얌냠 2022. 5. 27. 16:32

https://leetcode.com/problems/fibonacci-number/

dynamic programming day 1

 

Fibonacci Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

class Solution {
public:
    int fib(int n) {
        if (n < 2)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
};
class Solution {
public:
    int fib(int n) {
        
        int f[n+1];
        
        f[0]=0;
        
        if(n==0){
            return 0;
        }
        
        f[1]=1;
        
        for(int i=2;i<=n;i++){
            f[i] = f[i-1]+f[i-2];
        }
        
        return f[n];
        
        
    }
};
728x90
반응형