js实现数据结构

打印 上一主题 下一主题

主题 856|帖子 856|积分 2568

常见的数据结构

数组



  • 创建数组
    数组字面量[],new Array().fill()
    二维数组,两层循环创建

  • 头部添加unshift
    尾部添加push
    任意位置添加 splice(index,0,item)

  • 头部删除 shift
    尾部删除pop
    任意位置删除splice(index,num)


先进后出
push,pop
队列

先进先出
push,shift
链表

  1. class Node{
  2.         constructor(val)
  3.         {
  4.                 this.val=val;
  5.                 this.next=null;
  6.         }
  7. }
复制代码


  • 添加元素
  1. cur.next = pre.next
  2. pre.next = cur.next
复制代码


  • 删除元素
  1. pre.next = cur.next
复制代码
二叉树

  1. class TreeNode(){
  2.         constructor(val){
  3.                 this.val=val;
  4.                 this.left=null;
  5.                 this.right=null;
  6.         }
  7. }
复制代码


  • 遍历
    先序遍历,中序遍历,后序遍历
数组应用

map

空间换时间 – map
1. 两数之和 - 力扣(LeetCode)
双指针

有序 数组 对撞指针
88. 合并两个有序数组 - 力扣(LeetCode)
15. 三数之和 - 力扣(LeetCode)
字符串的应用



  • 反转字符串
    str.split("").reverse().join("")
  • 判断是否是回文字符串
    `str == str.split(“”).reverse().join(“”)
回文字符串

对称 + 双指针
LCR 019. 验证回文串 II - 力扣(LeetCode)
字符串匹配

  1.   
  2. class WordDictionary{
  3.     constructor(){
  4.         this.words={}
  5.     }
  6.     //add
  7.     addWord(word){
  8.       if(this.words[word.length]){
  9.         this.words[word.length].push(word)
  10.       }else{
  11.         this.words[word.length] = [word]
  12.       }
  13.     }
  14.     //search
  15.     search(word){
  16.         if(!this.words[word.length]){
  17.             return false;
  18.         }
  19.         const len = word.length;
  20.         //不包含.,则是普通字符
  21.         if(!word.includes(".")){
  22.             return this.words[len].includes(word);
  23.         }
  24.         //包含.则是正则表达式
  25.         const reg = new RegExp(word);
  26.         return this.words[len].some((item) => reg.test(item));
  27.     }
  28.   
  29. }
复制代码
正则表达式

test 是否匹配乐成
match 获取捕获结果
  1. /**
  2. * 实现一个atoi函数,使其能将字符串转换成整数
  3. */
  4. function atoi(str){
  5.     //去除空格
  6.     const reg = /\s*([-\+]?[0-9]*).*/
  7.     //得到捕获组
  8.     const groups = str.match(reg);
  9.     //最大值
  10.     const max = Math.pow(2,31) - 1;
  11.     //最小值
  12.     const min = -Math.pow(2,31);
  13.     //存储转化出来的数字
  14.     let targetNum = 0;
  15.     if(groups){
  16.         targetNum = parseInt(groups[1]);
  17.         if(isNaN(targetNum)){
  18.             targetNum =  0;
  19.         }
  20.     }
  21.   
  22.     if(targetNum > max){
  23.         return max;
  24.     }
  25.     if(targetNum < min){
  26.         return min;
  27.     }
  28.   
  29.     return targetNum;
  30.   
  31.   
  32. }
复制代码
链表应用

链表处理

哑节点
dummy = new ListNode() dummy.next = head
21. 合并两个有序链表 - 力扣(LeetCode)
83. 删除排序链表中的重复元素 - 力扣(LeetCode)
82. 删除排序链表中的重复元素 II - 力扣(LeetCode)
链表的反转

一次遍历 快慢指针
LCR 021. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
206. 反转链表 - 力扣(LeetCode)
链表成环

设置flag
141. 环形链表 - 力扣(LeetCode)
142. 环形链表 II - 力扣(LeetCode)
栈与队列

括号问题

20. 有效的括号 - 力扣(LeetCode)
每日温度

739. 每日温度 - 力扣(LeetCode)
最小栈

155. 最小栈 - 力扣(LeetCode)
用栈实现队列

232. 用栈实现队列 - 力扣(LeetCode)
滑动窗口

双端队列,头尾皆可进行删除添加
239. 滑动窗口最大值 - 力扣(LeetCode)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

半亩花草

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表