《穿越半径2》P8阶段开荒指南:四级升五级资源规划与任务链解析
2026/9/3 23:17:35
算法:
Java 实现
class Solution { public int fib(int N) { if (N <= 1) { return N; } if (N == 2) { return 1; } int current = 0; int prev1 = 1; int prev2 = 1; for (int i = 3; i <= N; i++) { current = prev1 + prev2; prev2 = prev1; prev1 = current; } return current; } }Python 实现
class Solution: def fib(self, N: int) -> int: if (N <= 1): return N if (N == 2): return 1 current = 0 prev1 = 1 prev2 = 1 # Since range is exclusive and we want to include N, we need to put N+1. for i in range(3, N+1): current = prev1 + prev2 prev2 = prev1 prev1 = current return current