classSolution{publicintfib(int n){// O(n) & O(1) 的动态规划int a =0;int b =1;int sum =0;for(int i =1; i <= n; i++){sum =(a + b)%1000000007;a = b;b = sum;}return a;}}
二刷
边界和返回值还是值得注意的
classSolution{publicintfib(int n){int pre =0, now =1, next =0;for(int i =1; i <= n; i++){next =(pre + now)%1000000007;pre = now;now = next;}return pre;}}
文章目录题目描述思路 & 代码二刷题目描述
涉及二进制,位运算跑不了~
思路 & 代码
既然是32位,那么通过一次遍历,每次判断一个位是否为1即可
public class Solution {// you need to treat n as an unsigned valuepu…