varstrToInt=function(str){//首先判断开头是不是空白字符if(str.match(/[^\s]|[^\d]/)){const[INT_MIN,INT_MAX]=[Math.pow(-2,31),Math.pow(2,31)-1]const s = str.trim();const res = s.match(/^[\+\-]?\d+/g);if(res >INT_MAX)returnINT_MAX;if(res <INT_MIN)returnINT_MIN;return res;}else{return0;}};
let slow = head
let fast = slow
while(k--){
fast = fast.next
}if(!fast)return slow
if(!fast.next)return slow.next
while(fast?.next){
fast = fast.next
slow = slow.next
}return slow.next
varMinStack=function(){this.stack =[]};/**
* @param {number} x
* @return {void}
*/MinStack.prototype.push=function(x){this.stack.push(x)};/**
* @return {void}
*/MinStack.prototype.pop=function(){this.stack.pop()};/**
* @return {number}
*/MinStack.prototype.top=function(){returnthis.stack[this.stack.length-1]};/**
* @return {number}
*/MinStack.prototype.min=function(){let min =Infinitythis.stack.forEach((num)=>{if(num<min){
min = num
}})return min
};
17 滑动窗口的最大值
constmaxSlidingWindow=(nums,k)=>{const queue =[]const res =[]let len = nums.length
for(let i =0; i < len; i++){while(queue.length && nums[queue.at(-1)]<= nums[i]){
queue.pop()}
queue.push(i)if(queue.length && queue[0]= i-k) queue.shift()if(i>= k-1){
res.push(nums[queue[0]])}}}
18 队列的最大值
varMaxQueue=function(){this.stack =[]this.max =-Infinity};/**
* @return {number}
*/MaxQueue.prototype.max_value=function(){if(this.stack.length)returnthis.max
return-1};/**
* @param {number} value
* @return {void}
*/MaxQueue.prototype.push_back=function(value){this.stack.push(value)if(value >this.max)this.max = value
};/**
* @return {number}
*/MaxQueue.prototype.pop_front=function(){if(this.stack.length){const res =this.stack.shift()if(res ===this.max){this.max = Math.max(...this.stack)}return res
}return-1};
19 顺时针打印矩阵
varspiralOrder=function(matrix){if(!matrix.length)return[]let row = matrix.length
let col = matrix[0].length
const res =[]
size = row*col
let t =0, r = col-1, b = row-1, l =0while(res.length!==size){//从左往右for(let i = l;i<=r;i++) res.push(matrix[t][i])
t++//从左到右可能便利完的矩阵//[[1,2,3],[4,5,6],[7,8,9]]//从上往下for(let i = t; i<=b;i++) res.push(matrix[i][r])
r--//从上到下可能遍历完的矩阵//[[7],[8],[9]]//到这里可能就遍历完了if(res.length===size)break//从左往右for(let i = r; i>=l; i--) res.push(matrix[b][i])
b--;//从下往上for(let i = b;i>=t; i--) res.push(matrix[i][l])
l++}return res
};
20 栈的压入、弹出序列
varvalidateStackSequences=function(pushed, popped){let stack =[]let i =0//模拟入栈出栈for(item of pushed){
stack.push(item)while(stack.length && stack.at(-1)== popped[i]){
stack.pop()
i++}}return!stack.length
};
21 数组中的重复数字
varfindRepeatNumber=function(nums){let map =newMap()let res
for(num of nums){if(map.get(num)){
res = num
break}
map.set(num,1)}return res
};
22 在排序数组中查找数字I
//法一varsearch=function(nums, target){let res =0for(num of nums){if(num===target)res++}return res
};//法二varsearch=function(nums, target){return nums.filter(item=> item === target).length
};
23 0~n-1中缺失的数字
//等差数列前n项和varmissingNumber=function(nums){const n = nums.length+1let total = Math.floor(n*(n-1)/2)let res = nums.reduce((prev,curr)=> prev+curr,0)return total - res
};