常见算法
// 反转链表
function ListNode(val, next) {
this.val = val;
this.next = next || null;
}
cn = new ListNode(3, null);
bn = new ListNode(2, cn);
an = new ListNode(1, bn)
var reverseList = function(head) {
let pre=null, cur=head;
while(cur) {
const next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
};
reverseList(an);
// 斐波那契数
function fib(n=0) {
if(n===0) return 0;
if(n===1) return 1;
return fib(n-1) + fib(n-2);
}
var fib = function(n) {
if (n < 2) {
return n;
}
let p = 0, q = 0, r = 1;
for (let i = 2; i <= n; i++) {
p = q;
q = r;
r = p + q;
}
return r;
};
// 快排
function quickSort(arr = []) {
const rec = (arr) => {
if (arr.length <= 1) return arr;
const left = [];
const right = [];
const mid = arr[0];
for (let i = 1; i < arr.length; i++) {
const cur = arr[i];
if (cur < mid) {
left.push(cur);
} else {
right.push(cur);
}
}
return [...quickSort(left), mid, ...quickSort(right)];
};
return rec(arr);
}
quickSort([1, 3, 2]);
function quickSort(arr=[]) {
if(arr.length <=1)return arr;
let [left, right] = [[], []];
const cur = arr.shift();
arr.forEach(item => item < cur ? left.push(item): right.push(item));
return [...quickSort(left), cur, ...quickSort(right)]
}
// 选择排序
function selectionSort(arr = []) {
for (let i = 0; i < arr.length; i++) {
let minIdx = i;
for (let j = i; j < arr.length; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
const temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
return arr;
}
selectionSort([1, 3, 2]);
// 插入排序
function insertionSort(arr = []) {
for (let i = 0; i < arr.length; i++) {
const temp = arr[i];
let j = i;
while (j > 0) {
if (arr[j - 1] > temp) {
arr[j] = arr[j - 1];
} else {
break;
}
j -= 1;
}
arr[j] = temp;
}
return arr;
}
insertionSort([1, 3, 2]);
// 二分查找
function binarySearch(nums=[], target) {
let left = 0, right = nums.length - 1; // [left, right]
while(left <= right) {
const mid = Math.floor((left + right) / 2);
if(target < nums[mid]){
right = mid - 1;
} else if(target > nums[mid]) {
left = mid +1;
} else {
return mid
}
}
return -1;
}
// 合并有序的两个链表
var mergeTwoLists = function (list1, list2) {
const res = new ListNode(0);
let p = res;
while (list1 && list2) {
if (list1.val < list2.val) {
p.next = list1;
list1 = list1.next;
} else {
p.next = list2;
list2 = list2.next;
}
p = p.next;
}
list1 && (p.next = list1);
list2 && (p.next = list2);
return res.next;
};
// LRU
class LRU {
constructor(capacity) {
this.capacity = capacity;
this.map = new Map();
}
get(key) {
if (this.map.has(key)) {
const value = this.map.get(key);
// 先删除
this.map.delete(key);
// 再添加
this.map.set(key, value);
return value;
}
return -1;
}
put(key, value) {
// 先删除
if (this.map.has(key)) {
this.map.delete(key);
}
// 再添加
this.map.set(key, value);
// 容量控制
if (this.map.size > this.capacity) {
// 迭代器的使用,删除第一个
this.map.delete(this.map.keys().next().value);
}
}
}
题目-字符串相关

// 题解
const inputList = [123, -123, 120, 2147483648, -2147483648];
function adi(num) {
const MAX = 2 ** 32;
const MIN = 0 - MAX;
const isMinus = num < 0;
isMinus && (num = 0 - num);
let rest = String(num).split('').reverse().join('');
const result = isMinus ? 0 - parseInt(rest, 10): parseInt(rest, 0);
if(result >= MAX || result <= MIN) {return 0}
return result;
}
inputList.forEach(num => {
console.log(adi(num));
})