LeetCode 509. Fibonacci Number Posted on 2021-01-05 Edited on 2024-11-24 In LeetCode 123456789101112131415class Solution {public: int fib(int n) { if (n < 2) { return n; } int p = 0, q = 0, r = 1; for (int i = 2; i <= n; ++i) { p = q; q = r; r = p + q; } return r; }};