Skip to content

Latest commit

 

History

History
16 lines (15 loc) · 329 Bytes

1137. 第 N 个泰波那契数.md

File metadata and controls

16 lines (15 loc) · 329 Bytes
class Solution {
    public int tribonacci(int n) {
        if (n <= 1) return n;
        if (n == 2) return 1;
        int p1 = 0, p2 = 1, p3 = 1;
        for (int i = 2; i < n; i++) {
            p3 = p3 + p2 + p1;
            p2 = p3 - p2 - p1;
            p1 = p3 - p2 - p1;
        }
        return p3;
    }
}