常见的数据结构
数组
- 创建数组
数组字面量[],new Array().fill()
二维数组,两层循环创建
- 增
头部添加unshift
尾部添加push
任意位置添加 splice(index,0,item)
- 删
头部删除 shift
尾部删除pop
任意位置删除splice(index,num)
栈
先进后出
push,pop
队列
先进先出
push,shift
链表
- class Node{
- constructor(val)
- {
- this.val=val;
- this.next=null;
- }
- }
复制代码
- cur.next = pre.next
- pre.next = cur.next
复制代码
二叉树
- 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)
字符串匹配
-
- 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 获取捕获结果
- /**
- * 实现一个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)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |