首页 > 编程语言 >LeetCode 斐波那契数算法题解 All In One

LeetCode 斐波那契数算法题解 All In One

时间:2022-10-02 04:33:06浏览次数:59  
标签:case fib const 契数 题解 斐波 result test input

LeetCode 斐波那契数算法题解 All In One

  1. Fibonacci Number

  2. 斐波那契数

最佳实践

性能优化


"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-09-30
 * @modified
 *
 * @description 509. Fibonacci Number
 * @description 509. 斐波那契数
 * @difficulty Easy
 * @time_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/fibonacci-number/
 * @link https://leetcode.cn/problems/fibonacci-number/
 * @solutions
 *
 * @best_solutions
 *
 * @refs https://xiaochen1024.com/series/6196129fc1553b002e57bef5/619621d3c1553b002e57bef8
 *
 *
 */

const log = console.log;

function fib(n) {
  if(n === 0) {
    return 0;
  }
  if(n === 1 || n === 2) {
    return 1;
  }
  let n1 = 1;
  let n2 = 1;
  while (n > 2) {
    // ES6 swap
    [n1, n2] = [n2, (n1 + n2)];
    // ES5 swap
    // const temp = n1 + n2;
    // n1 = n2;
    // n2 = temp;
    n -= 1;
  }
  return n2;
}

// 测试用例 test cases
const testCases = [
  {
    input: 0,
    result: 0,
    desc: 'value equal to 0',
  },
  {
    input: 1,
    result: 1,
    desc: 'value equal to 1',
  },
  {
    input: 2,
    result: 1,
    desc: 'value equal to 1',
  },
  {
    input: 5,
    result: 5,
    desc: 'value equal to 5',
  },
  {
    input: 8,
    result: 21,
    desc: 'value equal to 21',
  },
  {
    input: 32,
    result: 2178309,
    desc: 'value equal to 2178309',
  },
  {
    input: 64,
    result: 10610209857723,
    desc: 'value equal to 10610209857723',
  },
  {
    input: 80,
    result: 23416728348467685,
    desc: 'value equal to 23416728348467685',
  },
  {
    input: 90,
    result: 2880067194370816120,
    desc: 'value equal to 2880067194370816120',
  },
  // {
  //   input: 100,
  //   result: 354224848179261915075,
  //   desc: 'value equal to 354224848179261915075',
  // },
];

log(`fib(80) ??? 23416728348467685 => 23416728348467684 =`, 23416728348467685 === 23416728348467684);
// fib(90) ??? 2880067194370816120 => 2880067194370816000 = true

log(`fib(90) ??? 2880067194370816120 => 2880067194370816000 =`, 2880067194370816000 === 2880067194370816120);
// fib(90) ??? 2880067194370816120 => 2880067194370816000 = true

log(`fib(100) 超出最大范围了 ❌ 354224848179261915075 => 354224848179262000000 =`, 354224848179261915075 === 354224848179262000000, `❌\n`);
// fib(100) 超出最大范围了 ❌ 354224848179261915075 => 354224848179262000000 = false

// 超出最大范围了 ❌
// fib(100) => 354224848179261915075 Math (https://r-knott.surrey.ac.uk/Fibonacci/fibtable.html)
// fib(100) => 354224848179262000000 Chrome ❌
// fib(100) => 354224848179262000000 Node.js ❌

// $node ./fib.js

for (const [i, testCase] of testCases.entries()) {
  const result = fib(testCase.input);
  // log(`test case ${i} result: `, result === testCase.result ? `✅ passed` : `❌ failed result = ${result}`, testCase.result);
  log(`test case ${i} result: `, result === testCase.result ? `✅ passed` : `❌ failed`, result);
}

// const testCases = [...new Uint8Array(100)].map((item, i) => ({
//   input: i,
//   result: fib(i),
//   desc: `value equal to ${fib(i)}`,
// }));
// ...spread 突破, 2**8 = 256 的 number 最大值的大小限制 ✅
// const testCases = [...new Uint8Array(100)].map((item, i) => i + 1);
// for (const [i, testCase] of testCases.entries()) {
//   const result = fib(testCase);
//   log(`

标签:case,fib,const,契数,题解,斐波,result,test,input
From: https://www.cnblogs.com/xgqfrms/p/16748194.html

相关文章

  • 2022 牛客多校题解
    2022牛客多校题解Contest1JContest2JContest3HHack(SAM)枚举B中的右端点,查询最长在A串中向右可以延伸多长。考虑对A串建立一个SAM,然后对于B串从左到右跑SAM的D......
  • 洛谷 P2709 小B的询问 题解
    莫队板子。//P2709小B的询问#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintMAXN=50005;structQuery{ intl,r,id;}q[MAXN];in......
  • The 2022 ICPC Asia Regionals Online Contest (II)部分题解
    ......
  • LeetCode 无重复字符的最长子串算法题解 All In One
    LeetCode无重复字符的最长子串算法题解AllInOnejs/ts实现无重复字符的最长子串无重复字符的最长子串原理图解滑动窗口"usestrict";/****@authorx......
  • Even Number Addicts - 题解【动态规划/记忆化搜索】
    题面本题是CodeforcesGlobalRound22的C题。原题链接见:C.EvenNumberAddicts。下面搬运一下题面:DescriptionAliceandBobareplayingagameonasequence\(a_......
  • LJT的书库题解
    原题链接题目难度较低,看懂题目后特别好想。注意到题目中说的,一个藏书室最多与两个相同的藏书室相连。那么含有所需书的藏书室是树上的一条链。但是,书的本数未知,且链的两......
  • 【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题
    问题描述当本地Node.js(Linux+Node.js+npm+yarn)部署上云,选择AzureAppServiceforLinux环境。但是在部署时,遇见了以下三个问题:问题一:使用VSCode进行部署,部署速......
  • CF600C题解
    题意有一串字符串\(S\),设改变\(S\)中的任意一个字符称为1次转变(交换任意2个字符不算转变次数),求在操作最少的情况下可得到的回文字符串正文♦算法:找规律♦准备......
  • LeetCode剑指 Offer II 093 最长斐波那契数列
    LeetCode剑指OfferII093最长斐波那契数列classSolution:deflenLongestFibSubseq(self,arr:List[int])->int:n,loc,ans=len(arr),{},0......
  • 题解 [POI2010]ZAB-Frog
    很厉害的题。倍增和单调队列。这是zpl新手向算法第二弹,第一弹可以看小挖的核燃料填充我会尽量讲的比较细致。同第一弹,尽量配合代码食用。这道题的题目描述写的不是......