标题没有错哈哈
还多了负一和零,想概括得更全面一点~
目录更新如下
function threeSum(nums) {
const ans = [];
if (nums.length < 3 || nums == null) return ans;
const n = nums.length;
nums.sort((a, b) => a - b);
for (let i = 0; i < n; i++) {
if (nums[i] > 0) break;
if (i > 0 && nums[i] === nums[i - 1]) continue;
let L = i + 1;
let R = n - 1;
while (L < R) {
const sum = nums[i] + nums[L] + nums[R];
if (sum === 0) {
ans.push([nums[i], nums[L], nums[R]]);
while (L > 0 && nums[L] === nums[L + 1]) L++;
while (R < n - 1 && nums[R] === nums[R - 1]) R--;
L++;
R--;
} else if (sum < 0) {
L++;
} else {
R--;
}
}
}
return ans;
}
var isPalindrome = function (s) {
let pattern = /[^a-z]/gi;
s = s.replace(pattern, "").toLocaleLowerCase();
if (!s.length) {
return true;
}
let j = s.length - 1;
for (let i = 0; i <= j; i++) {
if (i === j) return true;
if (s[i] === s[j]) {
j--;
} else {
return false;
}
}
return true;
};
var mergeTwoLists = function (list1, list2) {
const res = new ListNode(0);
let p = res;
let p1 = list1;
let p2 = list2;
while (p1 && p2) {
if (p1.val < p2.val) {
p.next = p1;
p1 = p1.next;
} else {
p.next = p2;
p2 = p2.next;
}
p = p.next;
}
if (p1) {
p.next = p1;
}
if (p2) {
p.next = p2;
}
return res.next;
};
class Heap {
data = [];
length = 0;
isMax;
constructor(arr, isMax = true) {
this.isMax = isMax;
this.buildHeap(arr);
}
compare(i, j) {
if (this.isMax) {
return this.data[i] >= this.data[j];
} else {
return this.data[i] <= this.data[j];
}
}
swap(i, j) {
const temp = this.data[i];
this.data[i] = this.data[j];
this.data[j] = temp;
}
insert(value) {
this.data.push(value);
this.length++;
this.heapify_up();
}
heapify_up() {
const index = this.data.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (this.compare(parentIndex, index)) {
break;
}
this.swap(index, parentIndex);
index = parentIndex;
}
}
extract() {
if (this.length === 0) return undefined;
if (this.length === 1) {
this.length--;
return this.data.pop();
}
const topValue = this.data[0];
this.length--;
this.heapify_down(0);
return topValue;
}
heapify_down(index) {
while (2 * index + 1 < this.length) {
let leftindex = 2 * index + 1;
let rightIndex = 2 * index + 2;
let largerIndex = leftindex;
if (rightIndex <= this.length && this.compare(leftindex, rightIndex)) {
largerIndex = rightIndex;
}
if (this.compare(index, largerIndex)) break;
this.swap(largerIndex, index);
index = largerIndex;
}
}
buildHeap(arr) {
this.data = arr;
this.length = arr.length;
const start = Math.floor((this.length - 1) / 2);
for (let i = start; start >= 0; start--) {
this.heapify_down(i);
}
}
peek() {
return this.data[0];
}
size() {
return this.length;
}
isEmpty() {
return this.length === 0;
}
}
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
class Heap {
data = [];
length = 0;
swap(i, j) {
const temp = this.data[i];
this.data[i] = this.data[j];
this.data[j] = temp;
}
insert(value) {
this.data.push(value);
this.length++;
this.heapify_up();
}
heapify_up() {
let index = this.data.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (this.data[parentIndex] <= this.data[index]) {
break;
}
this.swap(index, parentIndex);
index = parentIndex;
}
}
extract() {
if (this.length === 0) return undefined;
if (this.length === 1) {
this.length--;
return this.data.pop();
}
const topValue = this.data[0];
this.data[0] = this.data.pop();
this.length--;
this.heapify_down(0);
return topValue;
}
heapify_down(index) {
while (2 * index + 1 < this.length) {
let leftindex = 2 * index + 1;
let rightIndex = 2 * index + 2;
let largerIndex = leftindex;
if (rightIndex <= this.length && this.data[leftindex] > this.data[rightIndex]) {
largerIndex = rightIndex;
}
if (this.data[index] <= this.data[largerIndex]) break;
this.swap(largerIndex, index);
index = largerIndex;
}
}
buildHeap(arr) {
this.data = arr;
this.length = arr.length;
const start = Math.floor((this.length - 1) / 2);
for (let i = start; start >= 0; start--) {
this.heapify_down(i);
}
}
peek() {
return this.data[0];
}
size() {
return this.length;
}
isEmpty() {
return this.length === 0;
}
}
var findKthLargest = function (nums, k) {
const heap = new Heap();
nums.forEach((n) => {
heap.insert(n);
if (heap.size() > k) {
const res = heap.extract();
console.log(res);
}
});
return heap.peek();
};
class Heap {
data = [];
length = 0;
swap(i, j) {
const temp = this.data[i];
this.data[i] = this.data[j];
this.data[j] = temp;
}
insert(value) {
this.data.push(value);
this.length++;
this.heapify_up();
}
heapify_up() {
let index = this.data.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (this.data[parentIndex] && this.data[parentIndex].value <= this.data[index].value) {
break;
}
this.swap(index, parentIndex);
index = parentIndex;
}
}
extract() {
if (this.length === 0) return undefined;
if (this.length === 1) {
this.length--;
return this.data.pop();
}
const topValue = this.data[0];
this.data[0] = this.data.pop();
this.length--;
this.heapify_down(0);
return topValue;
}
heapify_down(index) {
while (2 * index + 1 < this.length) {
let leftindex = 2 * index + 1;
let rightIndex = 2 * index + 2;
let largerIndex = leftindex;
if (this.data[rightIndex] && rightIndex <= this.length && this.data[leftindex].value > this.data[rightIndex].value) {
largerIndex = rightIndex;
}
if (this.data[index].value <= this.data[largerIndex].value) break;
this.swap(largerIndex, index);
index = largerIndex;
}
}
buildHeap(arr) {
this.data = arr;
this.length = arr.length;
const start = Math.floor((this.length - 1) / 2);
for (let i = start; start >= 0; start--) {
this.heapify_down(i);
}
}
peek() {
return this.data[0];
}
size() {
return this.length;
}
isEmpty() {
return this.length === 0;
}
}
var topKFrequent = function (nums, k) {
const map = new Map();
nums.forEach((num) => {
map.set(num, map.has(num) ? map.get(num) + 1 : 1);
});
const heap = new Heap();
map.forEach((value, key) => {
heap.insert({ key, value });
if (heap.size() > k) {
heap.extract();
}
});
return heap.data.map((item) => item.key);
};
输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
class Heap {
data = [];
length = 0;
swap(i, j) {
const temp = this.data[i];
this.data[i] = this.data[j];
this.data[j] = temp;
}
insert(value) {
this.data.push(value);
this.length++;
this.heapify_up();
}
heapify_up() {
let index = this.data.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (this.data[parentIndex] && this.data[parentIndex].val <= this.data[index].val) {
break;
}
this.swap(index, parentIndex);
index = parentIndex;
}
}
extract() {
if (this.length === 0) return undefined;
if (this.length === 1) {
this.length--;
return this.data.pop();
}
const topValue = this.data[0];
this.data[0] = this.data.pop();
this.length--;
this.heapify_down(0);
return topValue;
}
heapify_down(index) {
while (2 * index + 1 < this.length) {
let leftindex = 2 * index + 1;
let rightIndex = 2 * index + 2;
let largerIndex = leftindex;
if (this.data[rightIndex] && rightIndex <= this.length && this.data[leftindex].val > this.data[rightIndex].val) {
largerIndex = rightIndex;
}
if (this.data[index].val <= this.data[largerIndex].val) break;
this.swap(largerIndex, index);
index = largerIndex;
}
}
buildHeap(arr) {
this.data = arr;
this.length = arr.length;
const start = Math.floor((this.length - 1) / 2);
for (let i = start; start >= 0; start--) {
this.heapify_down(i);
}
}
peek() {
return this.data[0];
}
size() {
return this.length;
}
isEmpty() {
return this.length === 0;
}
}
var mergeKLists = function (lists) {
const res = new ListNode(0);
let p = res;
const heap = new Heap();
lists.forEach((l) => {
if (l) heap.insert(l);
});
while (heap.size()) {
const n = heap.extract();
p.next = n;
p = p.next;
if (n.next) heap.insert(n.next);
}
return res.next;
};
// 交集
var intersection = function (nums1, nums2) {
const res = new Set();
const arr1 = new Set(nums1);
nums2.forEach((num) => {
if (arr1.has(num)) res.add(num);
});
return Array.from(res);
};
// 并集
var getUnion = function (nums1, nums2) {
const arr1 = new Set(nums1);
nums2.forEach((num) => {
arr1.add(num);
});
return Array.from(arr1);
};
var twoSum = function (nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const num1 = nums[i];
const num2 = target - num1;
if (map.has(num2)) {
return [i, map.get(num2)];
}
map.set(num1, i);
}
};
var lengthOfLongestSubstring = function (s) {
let l = 0;
let res = 0;
const map = new Map();
for (let r = 0; r < s.length; r++) {
if (map.has(s[r]) && map.get(s[r]) >= 1) {
l = map.get(s[r]) + 1;
}
res = Math.max(res, r - l + 1);
map.set(s[r], r);
}
return res;
};
var minWindow = function (s, t) {
let l = 0;
let r = 0;
const need = new Map();
let res = "";
for (const c of t) {
need.set(c, need.has(c) ? need.get(c) + 1 : 1);
}
let needType = need.size;
while (r < s.length) {
if (need.has(s[r])) {
need.set(s[r], need.get(s[r]) - 1);
if (need.get(s[r]) === 0) {
needType -= 1;
}
}
while (needType === 0) {
const newStr = s.substring(l, r + 1);
if (!res || newStr.length < res.length) res = newStr;
if (need.has(s[l])) {
need.set(s[l], need.get(s[l]) + 1);
if (need.get(s[l]) === 1) {
needType += 1;
}
}
l+=1;
}
r+=1;
}
return res
};
class URLCache {
length = 0;
data = new Map();
constructor(length) {
this.length = length;
}
set(key, value) {
const data = this.data;
if (data.has(key)) {
data.delete(key);
}
data.set(key, value);
if (data.size > this.length) {
const delKey = data.keys().next().value;
data.delete(delKey);
}
}
get(key) {
const data = this.data;
if (!data.has(key)) {
return null;
}
const value = data.get(key);
data.delete(key);
data.set(key, value);
return value;
}
}
- vue3 的 diff 算法中,最长自增子序列(贪心 + 二分查找)
- 需要用到子问题的解,选择最优的,从而知道整个问题的解
- 核心思想
- 将问题划分为若干个子问题,在子问题的基础上初步构建出原问题的解
- 求解步骤
- 定义状态
- 确定状态转移方程
- 初始化状态
- 计算最终的结果
- F0 = 0,F1 = 1
- F2 = F0 + F1
// 一、递归求解
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
// 二、动态规划
function fib(n) {
const dp = [];
dp[0] = 0;
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// 三、状态压缩
function fib(n) {
if (n <= 1) return n;
let prev = 0;
let cur = 1;
for (let i = 2; i <= n; i++) {
const newValue = prev + cur;
prev = cur;
cur = newValue;
}
return cur;
}
function jump(n){
const dp = [];
dp[0] = 1;
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
只能买卖一次
function maxProfit(price) {
if (price.length <= 1) return 0;
const n = price.length;
const dp = [];
dp[0] = 0;
let minPrice = price[0];
for (let i = 1; i <= n; i++) {
dp[i] = Math.max(dp[i - 1], price - minPrice);
minPrice = Math.min(minPrice, price[i]);
}
return dp[n - 1];
}
function maxProfit(price) {
const n = price.length;
if (n <= 1) return 0;
let prev = 0;
let minPrice = price[0];
for (let i = 1; i <= n; i++) {
prev = Math.max(prev, price[i] - minPrice);
minPrice = Math.min(minPrice, price[i]);
}
return prev
}
var maxSubArray = function (nums) {
const n = nums.length;
if (n <= 1) return nums[0];
const dp = [];
dp[0] = nums[0];
for (let i = 1; i < n; i++) {
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
}
return Math.max(...dp);
};
var maxSubArray = function (nums) {
const n = nums.length;
if (n <= 1) return nums[0];
let max = nums[0];
let prev = max;
for (let i = 1; i < n; i++) {
prev = Math.max(prev + nums[i], nums[i]);
max = Math.max(prev, max);
}
return max;
};
相邻房间不能偷
var rob = function(nums) {
const n = nums.length
if (n < 0) return 0
const dp = []
dp[0] = nums[0]
dp[1] = Math.max(dp[0], nums[1])
for (let i = 2; i < n; i++){
dp[i] = Math.max(dp[i-1],dp[i-2] + nums[i])
}
return dp[n-1]
};
房间围成了一个环
var rob = function (nums) {
const n = nums.length;
if (n < 0) return 0;
if (n === 1) return nums[0];
const result1 = robRange(nums, 0, n - 1);
const result2 = robRange(nums, 1, n);
return Math.max(result1, result2);
};
var robRange = function (nums, start, end) {
if (start === end) return nums[start];
const dp = [];
dp[start] = nums[start];
dp[start + 1] = Math.max(dp[start], nums[start + 1]);
for (let i = start + 2; i < end; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[end - 1];
};
function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n; i++) {
let swaped = false;
for (let j = 0; j < n - 1; j++) {
if (arr[j] > arr[j + 1]) {
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swaped = true;
}
}
if (!swaped) break;
}
return arr;
}
function selectionSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
let minIndex = 0;
for (let j = i; j < n; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
if (minIndex !== i) {
const temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
return arr;
}
function insertionSort(arr) {
const n = arr.length;
for (let i = 1; i < n; i++) {
let j = i - 1;
while (arr[j] > arr[i] && j >= 0) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = arr[i];
}
return arr;
}
function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
let leftArr = arr.slice(0, mid);
let rightArr = arr.slice(mid);
const newLeftArr = mergeSort(leftArr);
const newRightArr = mergeSort(rightArr);
let i = 0;
let j = 0;
let newArr = [];
while (i < newLeftArr.length && j < newRightArr.length) {
if (newLeftArr[i] <= newRightArr[j]) {
newArr.push(newLeftArr[i]);
i++;
} else {
newArr.push(newRightArr[j]);
j++;
}
}
if (i < newLeftArr.length) {
newArr.push(...newLeftArr.slice(i));
}
if (j < newRightArr.length) {
newArr.push(...newRightArr.slice(j));
}
return newArr;
}
在里面写一个递归
基本思路是将一个大数组分成两个小数组,然后递归地对两个小数组进行排序
通过选择一个基准元素,将数组分成左右两个部分,左部分的元素都小于或等于基准元素,右部分的元素都大于基准元素
对左右两部分分别进行递归调用快速排序,最终将整个数组排序
function quickSort(arr) {
partition(0, arr.length);
function partition(left, right) {
if (left >= right) return;
const pivot = arr[right];
let i = left;
let j = right - 1;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] < pivot) {
j--;
}
if (i <= j) {
const temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
const temp = arr[i];
arr[i] = arr[right];
arr[right] = temp;
partition(left, j);
partition(i + 1, right);
}
return arr;
}
function heapSort(arr) {
const n = arr.length;
const start = Math.floor(n / 2 - 1);
for (let i = start; i >= 0; i--) {
heapifyDown(arr, n, i);
}
for (let i = 0; i < n; i++) {
swap(arr, i, n - 1);
heapifyDown(arr, n, i);
}
return arr;
}
function heapifyDown(arr, n, index) {
while (2 * index + 1 < n) {
let leftIndex = 2 * index + 1;
let rightIndex = 2 * index + 2;
let largerIndex = leftIndex;
if (rightIndex && arr[rightIndex] > arr[leftIndex]) {
largerIndex = rightIndex;
}
if (arr[index] >= arr[largerIndex]) {
break;
}
swap(arr, index, largerIndex);
index = largerIndex;
}
}
function swap(arr, i, j) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
支持多次买卖
function maxProfit(price) {
let profit = 0
for (let i = 1; i < price.length; i++){
if (price[i] > price[i - 1]) {
profit += price[i]-price[i-1]
}
}
return profit
}
g 胃口 = [1,2,3], s 饼干 = [1,1]
var findContentChildren = function (g, s) {
const sortFunc = (a, b) => {
return a - b;
};
s.sort(sortFunc);
g.sort(sortFunc);
let count = 0;
s.forEach((n) => {
if (n >= g[count]) {
count++;
}
});
return count;
};