《剑指Offer》-10-斐波那契数列/青蛙跳台阶

斐波那契数列

	long long mod = 1e9 + 7;
	int fib(int n) {
		if (n < 2) return n;
		int prepre = 0, pre = 1, res;

		for (int i = 2; i <= n; i++) {
			res = (prepre % mod + pre % mod) % mod;
			prepre = pre;
			pre = res;
		}
		return res;
	}

感觉效率有点低

青蛙跳台阶

	long mod = 1e9 + 7;
	int numWays(int n) {
		if (n < 2)return 1;
		int prepre = 1, pre = 1, res;
		for (int i = 2; i <= n; i++) {
			res = (prepre % mod + pre % mod) % mod;
			prepre = pre;
			pre = res;
		}
		return res;
	}

这俩居然真的是一样的

posted @ 2023-02-16 05:09  YaosGHC  阅读(13)  评论(0编辑  收藏  举报