|
1 | | -学习笔记 |
| 1 | +学习笔记 |
| 2 | + |
| 3 | +# 第八周学习笔记 |
| 4 | + |
| 5 | +## 常见排序算法 |
| 6 | +```javascript |
| 7 | + |
| 8 | +// 冒泡排序 |
| 9 | +function bubbleSort(nums) { |
| 10 | + for (let i = 0; i < nums.length - 1; ++i) { |
| 11 | + for (let j = i + 1; j < nums.length; ++j) { |
| 12 | + if (nums[i] > nums[j]) { |
| 13 | + let temp = nums[i]; |
| 14 | + nums[i] = nums[j]; |
| 15 | + nums[j] = temp; |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// 快速排序 |
| 22 | +function quickSort(nums, left, right) { |
| 23 | + if (left < right) { |
| 24 | + let i = left, j = right, temp = nums[left]; |
| 25 | + while(i < j) { |
| 26 | + while(i < j && nums[j] > temp) j--; |
| 27 | + if (i < j) nums[i++] = nums[j]; |
| 28 | + while(i < j && nums[i] < temp) i++; |
| 29 | + if (i < j) nums[j--] = nums[i]; |
| 30 | + } |
| 31 | + nums[i] = temp; |
| 32 | + quickSort(nums, left, i - 1); |
| 33 | + quickSort(nums, i + 1, right); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// 归并排序 |
| 38 | +const mergeSort = (nums) => { |
| 39 | + if (nums.length <= 1) return nums |
| 40 | + let mid = Math.floor(nums.length/2), |
| 41 | + left = nums.slice(0, mid), |
| 42 | + right = nums.slice(mid) |
| 43 | + return merge(mergeSort(left), mergeSort(right)) |
| 44 | +} |
| 45 | + |
| 46 | +const merge(left, right) => { |
| 47 | + let result = [] |
| 48 | + while(left.length && right.length) { |
| 49 | + result.push(left[0] <= right[0] ? left.shift() : right.shift() |
| 50 | + } |
| 51 | + while(left.length) result.push(left.shift()) |
| 52 | + while(right.length) result.push(right.shift()) |
| 53 | + return result |
| 54 | +} |
| 55 | + |
| 56 | +``` |
| 57 | +
|
| 58 | +## LRU缓存算法 |
| 59 | + LRU 缓存算法的核心数据结构就是哈希链表,双向链表和哈希表的结合体 |
| 60 | +
|
| 61 | +```javascript |
| 62 | +/** |
| 63 | + * 使用 哈希表 + 双端链表 实现 |
| 64 | + */ |
| 65 | +class LinkedNode { |
| 66 | + constructor(key = 0, val = 0) { |
| 67 | + this.key = key |
| 68 | + this.val = val |
| 69 | + this.prev = null |
| 70 | + this.next = null |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +class LinkedList { |
| 76 | + constructor() { |
| 77 | + this.head = new LinkedNode() |
| 78 | + this.tail = new LinkedNode() |
| 79 | + this.head.next = this.tail |
| 80 | + this.tail.prev = this.head |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + insertFirst(node) { |
| 85 | + node.next = this.head.next |
| 86 | + node.prev = this.head |
| 87 | + this.head.next.prev = node |
| 88 | + this.head.next = node |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + remove(node) { |
| 93 | + node.prev.next = node.next |
| 94 | + node.next.prev = node.prev |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + removeLast() { |
| 99 | + if (this.tail.prev === this.head) return null |
| 100 | + let last = this.tail.prev |
| 101 | + this.remove(last) |
| 102 | + return last |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +/** |
| 108 | + * @param {number} capacity |
| 109 | + */ |
| 110 | +var LRUCache = function(capacity) { |
| 111 | + this.capacity = capacity |
| 112 | + this.keyNodeMap = new Map() |
| 113 | + this.cacheLink = new LinkedList() |
| 114 | +}; |
| 115 | + |
| 116 | + |
| 117 | +/** |
| 118 | + * @param {number} key |
| 119 | + * @return {number} |
| 120 | + */ |
| 121 | +LRUCache.prototype.get = function(key) { |
| 122 | + if (!this.keyNodeMap.has(key)) return -1 |
| 123 | + let val = this.keyNodeMap.get(key).val |
| 124 | + this.put(key, val) |
| 125 | + return val |
| 126 | +}; |
| 127 | + |
| 128 | + |
| 129 | +/** |
| 130 | + * @param {number} key |
| 131 | + * @param {number} value |
| 132 | + * @return {void} |
| 133 | + */ |
| 134 | +LRUCache.prototype.put = function(key, value) { |
| 135 | + let size = this.keyNodeMap.size |
| 136 | + if (this.keyNodeMap.has(key)) { |
| 137 | + this.cacheLink.remove(this.keyNodeMap.get(key)); |
| 138 | + --size |
| 139 | + } |
| 140 | + if (size >= this.capacity) { |
| 141 | + this.keyNodeMap.delete(this.cacheLink.removeLast().key) |
| 142 | + } |
| 143 | + let node = new LinkedNode(key, value) |
| 144 | + this.keyNodeMap.set(key, node) |
| 145 | + this.cacheLink.insertFirst(node) |
| 146 | +}; |
| 147 | + |
| 148 | +``` |
0 commit comments