程式語言 - LeetCode - C++ - 1137. N-th Tribonacci Number



參考資訊:
https://www.cnblogs.com/grandyang/p/14864666.html

題目:


解答:

class Solution {
public:
    int tribonacci(int n) {
        int c0 = 0;
        int c1 = 1;
        int c2 = 1;

        if (n < 2) {
            return n;
        }
        
        for (int i = 2; i < n; i++) {
            int t = c0;

            c0 = c1;
            c1 = c2;
            c2 = t + c0 + c1;
        }

        return c2;
    }
};