学习笔记学习笔记
主站博客
面试
前端
开发工具
Cesium
GitHub
主站博客
面试
前端
开发工具
Cesium
GitHub
  • 面试
  • 算法

    • hash哈希

      • lc-1-简-两数之和
    • top K

      • lc-215-数组中的第K个最大元素
      • lc-347-中-前 K 个高频元素
      • lc-LCR159-简-库存管理 III
    • 二叉树

      • lc-101-简-对称二叉树
      • lc-102-中-二叉树的层序遍历
      • lc-104-简-二叉树的最大深度
      • lc-105-中-从前序与中序遍历序列构造二叉树
      • lc-114-中-二叉树展开为链表
      • lc-226-简-翻转二叉树
      • lc-236-中-二叉树的最近公共祖先
    • 动态规划

      • lc-1277-中-统计全为 1 的正方形子矩阵
      • lc-221-中-最大正方形
      • lc-62-中-不同路径
      • lc-70-简-爬楼梯
      • lc-72-中-编辑距离
      • lc-746-简-使用最小花费爬楼梯
      • 背包-01背包
      • 背包-完全背包
    • 原地哈希

      • lc-33-中-搜索旋转排序数组
      • lc-442-中-数组中重复的数据
      • lc-448-简-找到所有数组中消失的数字
    • 图

      • lc-207-中-课程表
      • lc-997- 简-找到小镇的法官
    • 待分类

      • lc-11-中-盛最多水的容器
      • lc-121-简-买卖股票的最佳时机
      • lc-128-中-最长连续序列
      • lc-136-简-只出现一次的数字
      • lc-139-中-单词拆分
      • lc-152-中-乘积最大子数组
      • lc-20-简-有效的括号
      • lc-200-中-岛屿数量
      • lc-22-中-括号生成
      • lc-279-中-完全平方数
      • lc-31-中-下一个排列
      • lc-322-中-零钱兑换
      • lc-34-中-在排序数组中查找元素的第一个和最后一个位置
      • lc-39-中-组合总和
      • lc-416-中-分割等和子集
      • lc-42-难-接雨水
      • lc-437-中-路径总和 III
      • lc-438-中-找到字符串中所有字母异位词
      • lc-49-中-字母异位词分组
      • lc-53-中-最大子数组和
      • lc-55-中-跳跃游戏
      • lc-56-中-合并区间
      • lc-560-中-和为 K 的子数组
      • lc-647-中-回文子串
      • lc-79-中-单词搜索
    • 排序

      • 归并排序

        • 归并排序
      • 快排
      • 排序+双指针

        • lc- 524-中-通过删除字母匹配到字典里最长单词
        • lc-15-中-三数之和
        • lc-16-中-最接近的三数之和
        • lc-283-简-移动零
        • lc-3-中-无重复字符的最长子串
        • lc-75-中-颜色分类
      • 计数排序
    • 算法工具库js
    • 递归+回溯

      • lc-17-中-电话号码的字母组合
      • lc-77-中-组合
      • LCR-083-中-全排列-不重复
      • LCR-084-中-全排列-重复
    • 链表

      • lc-142-中-环形链表 II
      • lc-148-中-排序链表
      • lc-160-简-相交链表
      • lc-19-中-删除链表的倒数第 N 个结点
      • lc-287-中-寻找重复数
  • 手撕代码

    • 柯里化 Curring
    • h5 Evnet详解
    • promiseify化
    • script标签详解
    • vue-SSR服务端渲染
    • vue双向绑定
    • vue核心原理全解
    • 路由的实现
    • 原型链
    • 变量提升
    • 宏任务、微任务
    • 左右两栏布局
    • 异步
    • 微前端
    • 实现js的filter函数
    • 实现promise
    • 捕获、冒泡
    • js中new的本质
    • Object.xxx
    • this
    • 反射Reflect,、代理Proxy
    • 基础类型
    • 深拷贝-浅拷贝
    • 继承
    • 跨域
    • 闭包
    • 防抖-节流

算法工具库js

算法在本地测试的时候想,需要的一些工具,主要包括

  • 创建二叉树节点
  • 打印矩形
  • ...

arr2tree.js

二叉树和数组之间相互转换

function TreeNode(val, left, right) {
  this.val = val;
  this.left = left;
  this.right = right;
}

var generateTree = function (nums) {
  let nodeArr = new Array(nums.length);
  for (let i = nums.length - 1; i >= 0; i--) {
    let val = nums[i];
    if (val != null) {
      if (!nodeArr[i]) {
        nodeArr[i] = new TreeNode(val, null, null); // 第一次被初始化
      } else {
        nodeArr[i].val = val; // 在下面给 parentNode 设置左右孩子时候,已经 new 过了
      }
    }

    let curNode = nodeArr[i];
    let isLeft = i % 2 !== 0;
    if (i !== 0) {
      let parentNodeIndex = Math.floor((i - 0.1) / 2);
      if (!nodeArr[parentNodeIndex]) nodeArr[parentNodeIndex] = new TreeNode();

      let parentNode = nodeArr[parentNodeIndex];

      if (isLeft) {
        parentNode.left = curNode;
      } else {
        parentNode.right = curNode;
      }
    }
  }

  // console.log(JSON.stringify(nodeArr[0], null, 2));
  return nodeArr[0];
};

var getLevelTree = (root) => {
  const queue = [root];
  let numArr = [];

  while (queue.length > 0) {
    let node = queue.shift();
    // 这里所有空的子节点,都是null,出现 [-1, 0, 3, -2, 4, null, null, 8, null, null, null, null, null]
    numArr.push(node ? node.val : null);
    if (node) {
      queue.push(node.left);
      queue.push(node.right);
    }
  }

  // 删除后面的null: [-1, 0, 3, -2, 4, null, null, 8, null, null, null, null, null]
  while (numArr[numArr.length - 1] == null) {
    numArr.pop();
  }
  return numArr;
};

// console.log(getLevelTree(generateTree([-1, 0, 3, -2, 4, null, null, 8])));
// console.log(getLevelTree(generateTree([3, 1, 2, 3, 4, 5, 6, 7, 8])));

module.exports = {
  TreeNode,
  generateTree,
  getLevelTree
};

array2d.js

打印二维数组

function printMatrix(matrix) {
    for (let i = 0; i < matrix.length; i++) {
      let row = '';
      for (let j = 0; j < matrix[i].length; j++) {
        row += matrix[i][j] + ' '; // 使用空格分隔每个元素
      }
      console.log(row.trim()); // 打印每一行并移除末尾的空格
    }
  }
  
  module.exports = printMatrix;

chianList.js

链式结构转换

function ListNode(val, next) {
  this.val = val === undefined ? 0 : val;
  this.next = next === undefined ? null : next;
}

var generateChain = function (nums) {
  let preNode = null;
  let curNode = null;
  for (let i = nums.length - 1; i >= 0; i--) {
    let val = nums[i];

    curNode = new ListNode(val, null);
    if (preNode) {
      curNode.next = preNode;
    }
    preNode = curNode;
  }
  return curNode;
};

let getChainArray = (node) => {
  if (!node) {
    return null;
  }
  let arr = [];
  let curNode = node;
  arr.push(curNode.val);
  while (curNode.next) {
    curNode = curNode.next;
    arr.push(curNode.val);
  }

  return arr;
};

module.exports = {
  ListNode,
  generateChain,
  getChainArray,
};

logResult.js

打印结果,好像用处不大,忘记了

function getLogResultFn(fn) {
  return function () {
    console.log(fn(...arguments));
  };
}

module.exports = getLogResultFn;

memoryTime.js

内存、耗时检测,leetcode的有点不准

function usageSize() {
    const used = process.memoryUsage().heapUsed;
    return Math.round((used / 1024 ) * 100) / 100 + " kb";
}

let now = null

function load(params) {
    now =  performance.now()
}
function log(params) {
    console.log(`time : ${(performance.now() - now).toFixed(4)} ms, memory: ${usageSize()}`);
}

const memoryTime = {
    load,
    log
}

module.exports = memoryTime;
在 GitHub 上编辑此页
上次更新:
贡献者: 国wei