1 替换空格
var replaceSpace = function(s) {
const str = s.split(' ');
return str.join('%20')
};
2 左旋转字符串
var reverseLeftWords = function(s, n) {
const s1 = s.slice(n)
const s2 = s.slice(0,n)
return s1+s2
};
3 表示数值的字符串
var isNumber = function(s) {
const str = s.trim();
const reg = /^[\+\-]?((\d+(\.\d*)?)|(\.\d+))(e[\+\-]?\d+)?$/i;
return reg.test(str)
};
4 把字符串转换成整数
var strToInt = 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) return INT_MAX;
if (res < INT_MIN) return INT_MIN;
return res;
} else {
return 0;
}
};
5 从尾到头打印链表
var reversePrint = function(head) {
const arr = []
while(head?.next) {
arr.push(head.val);
head = head.next
}
if(head?.val || head?.val===0) arr.push(head.val);
return arr.reverse()
};
6 反转链表
var reverseList = function(head) {
let prev = null
let curr = head
while(curr) {
const next = curr.next
curr.next = prev
prev=curr
curr = next
}
return prev
};
7 复杂链表的复制
var copyRandomList = function(head) {
if(!head) return null
const map = new Map()
let prev = head
while(prev){
map.set(prev, new Node(prev.val))
prev = prev.next
}
map.set(null,null)
prev = head
while(prev) {
map.get(prev).next = map.get(prev.next)
map.get(prev).random = map.get(prev.random)
prev = prev.next
}
return map.get(head)
};
8 删除链表的节点
var deleteNode = function(head, val) {
if(!head) return null
let newNode = new ListNode(0,head)
let prev = newNode
let curr = head
while(curr){
if(curr.val === val) {
prev.next = curr.next
} else {
prev = curr
}
curr = curr.next
}
return newNode.next
};
9 链表中倒数第k个节点
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
10 合并两个排序的链表
let prev = new ListNode(null)
let curr = prev
while(l1 && l2){
if(l1?.val >= l2?.val){
prev.next = l2
l2 = l2?.next
prev = prev.next
} else {
prev.next = l1
l1 = l1?.next
prev = prev.next
}
}
if (l1) prev.next = l1
if (l2) prev.next = l2
return curr.next
11 两个链表的第一个公共节点
var getIntersectionNode = function(headA, headB) {
let prevA = headA
let prevB = headB
while(prevA !== prevB) {
prevA = prevA ? prevA.next : headB
prevB = prevB ? prevB.next : headA
}
return prevA
};
12 调整数组顺序使奇数位于偶数前面
var exchange = function(nums) {
const arr = []
nums.forEach((num)=>{
if(num%2 === 1) {
arr.unshift(num)
} else {
arr.push(num)
}
})
return arr
};
13 和为s的两个数字
var twoSum = function(nums, target) {
let l = 0
let r = nums.length-1
while(l!==r){
const res = nums[l]+nums[r]
if(res > target){
r--
} else if (res < target) {
l++
} else {
return [nums[l],nums[r]]
}
}
};
14 翻转单词顺序
var reverseWords = function(s) {
const arr = s.trim().split(' ').filter(item=>item)
return arr.reverse().join(' ')
};
15 用两个栈实现队列
var CQueue = function() {
this.stackA = []
this.stackB = []
};
CQueue.prototype.appendTail = function(value) {
this.stackA.push(value)
};
CQueue.prototype.deleteHead = function() {
if(this.stackB.length){
return this.stackB.pop()
} else {
while(this.stackA.length) {
this.stackB.push(this.stackA.pop())
}
if(!this.stackB.length){
return -1
} else {
return this.stackB.pop()
}
}
};
16 包含min函数的栈
var MinStack = function() {
this.stack = []
};
MinStack.prototype.push = function(x) {
this.stack.push(x)
};
MinStack.prototype.pop = function() {
this.stack.pop()
};
MinStack.prototype.top = function() {
return this.stack[this.stack.length-1]
};
MinStack.prototype.min = function() {
let min = Infinity
this.stack.forEach((num)=>{
if(num<min){
min = num
}
})
return min
};
17 滑动窗口的最大值
const maxSlidingWindow = (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 队列的最大值
var MaxQueue = function() {
this.stack = []
this.max = -Infinity
};
MaxQueue.prototype.max_value = function() {
if(this.stack.length) return this.max
return -1
};
MaxQueue.prototype.push_back = function(value) {
this.stack.push(value)
if (value > this.max) this.max = value
};
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 顺时针打印矩阵
var spiralOrder = 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 =0
while(res.length!==size) {
for(let i = l;i<=r;i++) res.push(matrix[t][i])
t++
for(let i = t; i<=b;i++) res.push(matrix[i][r])
r--
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 栈的压入、弹出序列
var validateStackSequences = 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 数组中的重复数字
var findRepeatNumber = function(nums) {
let map = new Map()
let res
for(num of nums){
if (map.get(num)){
res = num
break
}
map.set(num,1)
}
return res
};
22 在排序数组中查找数字I
var search = function(nums, target) {
let res = 0
for(num of nums){
if(num===target)res++
}
return res
};
var search = function(nums, target) {
return nums.filter(item => item === target).length
};
23 0~n-1中缺失的数字
var missingNumber = function(nums) {
const n = nums.length+1
let total = Math.floor(n*(n-1)/2)
let res = nums.reduce((prev,curr) => prev+curr, 0)
return total - res
};