js实现数据结构

常见的数据结构

数组

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

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

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

先进后出

push,pop

队列

先进先出

push,shift

链表

js 复制代码
class Node{
	constructor(val)
	{
		this.val=val;
		this.next=null;
	}
}
  • 添加元素
js 复制代码
cur.next = pre.next
pre.next = cur.next
  • 删除元素
js 复制代码
pre.next = cur.next

二叉树

js 复制代码
class TreeNode(){
	constructor(val){
		this.val=val;
		this.left=null;
		this.right=null;
	}
}
  • 遍历
    先序遍历,中序遍历,后序遍历

数组应用

map

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

双指针

有序 数组 对撞指针

88. 合并两个有序数组 - 力扣(LeetCode)
15. 三数之和 - 力扣(LeetCode)

字符串的应用

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

回文字符串

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

字符串匹配

js 复制代码
  

class WordDictionary{

    constructor(){

        this.words={}

    }

    //add

    addWord(word){

      if(this.words[word.length]){

        this.words[word.length].push(word)

      }else{

        this.words[word.length] = [word]

      }

    }

    //search

    search(word){

        if(!this.words[word.length]){

            return false;

        }

        const len = word.length;

        //不包含.,则是普通字符

        if(!word.includes(".")){

            return this.words[len].includes(word);

        }

        //包含.则是正则表达式

        const reg = new RegExp(word);

        return this.words[len].some((item) => reg.test(item));

    }

  

}

正则表达式

test 是否匹配成功
match 获取捕获结果

js 复制代码
/**

 * 实现一个atoi函数,使其能将字符串转换成整数

 */

function atoi(str){

    //去除空格

    const reg = /\s*([-\+]?[0-9]*).*/

    //得到捕获组

    const groups = str.match(reg);

    //最大值

    const max = Math.pow(2,31) - 1;

    //最小值

    const min = -Math.pow(2,31);

    //存储转化出来的数字

    let targetNum = 0;

    if(groups){

        targetNum = parseInt(groups[1]);

        if(isNaN(targetNum)){

            targetNum =  0;

        }

    }

  

    if(targetNum > max){

        return max;

    }

    if(targetNum < min){

        return min;

    }

  

    return targetNum;

  
  

}

链表应用

链表处理

哑节点
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)

相关推荐
gu209 分钟前
c#编程:学习Linq,重几个简单示例开始
开发语言·学习·c#·linq
lly20240610 分钟前
SQLite 删除表
开发语言
wjs202415 分钟前
HTML 字符实体
开发语言
GDAL17 分钟前
HTML 中的 Canvas 样式设置全解
javascript
二十雨辰24 分钟前
[Java基础]网络编程
java·开发语言
GDAL29 分钟前
HTML Canvas clip 深入全面讲解
前端·javascript·canvas
卑微的小鬼34 分钟前
数据库使用B+树的原因
数据结构·b树
GISer_Jing35 分钟前
Javascript排序算法(冒泡排序、快速排序、选择排序、堆排序、插入排序、希尔排序)详解
javascript·算法·排序算法
cookies_s_s35 分钟前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
AC使者38 分钟前
介绍 TensorFlow 的基本概念和使用场景。
开发语言·自然语言处理·sqlite·github