Skip to content

Commit 4bef67a

Browse files
committed
finish the 8 week homework
1 parent aaaf42c commit 4bef67a

7 files changed

Lines changed: 299 additions & 1 deletion

File tree

Week07/findWords.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* 单词搜索||
3+
* @param {character[][]} board
4+
* @param {string[]} words
5+
* @return {string[]}
6+
*/
7+
var findWords = function(board, words) {
8+
let trie = new Trie();
9+
for (let word of words) {
10+
trie.insert(word);
11+
}
12+
let res = [];
13+
let rows = board.length;
14+
if (rows == 0) return res;
15+
let cols = board[0].length;
16+
for (let i = 0; i < rows; ++i) {
17+
for (let j = 0; j < cols; ++j) {
18+
dfs(board, i, j, trie.root, res);
19+
}
20+
}
21+
return res;
22+
function dfs(board, row, col, node, res) {
23+
if (row < 0 || col < 0 || row >= board.length || col >= board[0].length) return;
24+
let cur = board[row][col];
25+
if (cur == '$' || node.children[cur.charCodeAt() - 97] == null) return;
26+
node = node.children[cur.charCodeAt() - 97];
27+
if (node.word != null) {
28+
res.unshift(node.word);
29+
node.word = null;
30+
}
31+
board[row][col] = '$';
32+
dfs(board, row, col + 1, node, res);
33+
dfs(board, row + 1, col, node, res);
34+
dfs(board, row, col - 1, node, res);
35+
dfs(board, row - 1, col, node, res);
36+
board[row][col] = cur;
37+
}
38+
};
39+
40+
/**
41+
* Initialize your data structure here.
42+
*/
43+
const TrieNode = function () {
44+
this.isEnd = false;
45+
this.word = null;
46+
this.children = [];
47+
for (let i = 0; i < 26; ++i) {
48+
this.children[i] = null;
49+
}
50+
}
51+
52+
var Trie = function() {
53+
this.root = new TrieNode();
54+
};
55+
56+
/**
57+
* Inserts a word into the trie.
58+
* @param {string} word
59+
* @return {void}
60+
*/
61+
Trie.prototype.insert = function(word) {
62+
if (!word) return false;
63+
let node = this.root;
64+
for (let i = 0; i < word.length; ++i) {
65+
if (!node.children[word[i].charCodeAt() - 97]) {
66+
node.children[word[i].charCodeAt() - 97] = new TrieNode();
67+
}
68+
node = node.children[word[i].charCodeAt() - 97];
69+
}
70+
node.isEnd = true;
71+
node.word = word;
72+
return true;
73+
};

Week08/NOTE.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,148 @@
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+
```

Week08/hammingWeight.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* 位1的个数
3+
* @param {number} n - a positive integer
4+
* @return {number}
5+
*/
6+
var hammingWeight = function(n) {
7+
let sum = 0;
8+
while (n != 0) {
9+
sum++;
10+
n &= n - 1;
11+
}
12+
return sum;
13+
};

Week08/isAnagram.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 有效的字母异位词
3+
* @param {string} s
4+
* @param {string} t
5+
* @return {boolean}
6+
*/
7+
var isAnagram = function(s, t) {
8+
if (s.length != t.length) return false;
9+
let map = new Array(26).fill(0);
10+
for (let i = 0; i < s.length; ++i) {
11+
map[s.charCodeAt(i) - 97]++;
12+
map[t.charCodeAt(i) - 97]--;
13+
}
14+
for (let num of map) {
15+
if (num != 0) return false;
16+
}
17+
return true;
18+
};

Week08/isPowerOfTwo.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* 2的幂
3+
* @param {number} n
4+
* @return {boolean}
5+
*/
6+
var isPowerOfTwo = function(n) {
7+
return n > 0 && (n & (n - 1)) == 0;
8+
};

Week08/relativeSortArray.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 数组的相对排序
3+
* @param {number[]} arr1
4+
* @param {number[]} arr2
5+
* @return {number[]}
6+
*/
7+
var relativeSortArray = function(arr1, arr2) {
8+
let count = {};
9+
for (let num of arr1) {
10+
count[num] = count[num] ? count[num] + 1 : 1;
11+
}
12+
let index = 0;
13+
for (let num of arr2) {
14+
while (count[num] > 0) {
15+
count[num]--;
16+
arr1[index++] = num;
17+
}
18+
}
19+
for (let num in count) {
20+
while(count[num] > 0) {
21+
arr1[index++] = num;
22+
count[num]--;
23+
}
24+
}
25+
return arr1;
26+
};

Week08/reverseBits.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* 颠倒二进制位
3+
* @param {number} n - a positive integer
4+
* @return {number} - a positive integer
5+
*/
6+
var reverseBits = function(n) {
7+
n = (n >>> 16) | (n << 16)
8+
n = ((n & 0xff00ff00) >>> 8) | ((n & 0x00ff00ff) << 8)
9+
n = ((n & 0xf0f0f0f0) >>> 4) | ((n & 0x0f0f0f0f) << 4)
10+
n = ((n & 0xcccccccc) >>> 2) | ((n & 0x33333333) << 2)
11+
n = ((n & 0xaaaaaaaa) >>> 1) | ((n & 0x55555555) << 1)
12+
return n >>> 0
13+
};

0 commit comments

Comments
 (0)