Skip to content

Commit adbe912

Browse files
committed
Save changes
1 parent f790fad commit adbe912

18 files changed

+78
-143
lines changed

implementations/hash_tables/challenges/design-hashset.js renamed to implementations/hashmap/challenges/design-hashset.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,33 @@ class MyHashSet {
1010
}
1111

1212
add(key) {
13-
let idx = this._hash(key),
14-
node = this.set[idx];
13+
let idx = this._hash(key), node = this.set[idx];
1514
if (!node) this.set[idx] = new Node(key);
1615
else {
17-
while (node.next) node = node.next;
16+
while (node.next && node.value !== key) node = node.next;
17+
if (node.value === key) return;
1818
node.next = new Node(key);
1919
}
2020
this.size++;
2121
if (this.size > this.max * 0.7) this._rehash();
2222
}
2323

2424
remove(key) {
25-
let idx = this._hash(key),
26-
node = this.set[idx];
27-
console.log(idx, node);
25+
let idx = this._hash(key), node = this.set[idx];
2826
if (!node) return;
2927
else if (node.value === key) this.set[idx] = node.next;
3028
else {
3129
while (node.next && node.next.value !== key) node = node.next;
3230
if (!node.next) return;
3331
node.next = node.next.next;
3432
}
35-
console.log(this.set[idx]);
3633
this.size--;
3734
if (this.size < this.max * 0.2) this._rehash();
3835
}
3936

4037
contains(key) {
4138
let node = this.set[this._hash(key)];
42-
while (node && node.value !== key) node = node.next;
39+
while (node && node.value !== key) node = node.next;
4340
return !!node;
4441
}
4542

@@ -58,9 +55,7 @@ class MyHashSet {
5855
}
5956

6057
_nextPrime() {
61-
const min = this.max * 1.5,
62-
max = this.max * 3;
63-
const sieve = new Array(max + 1).fill(true);
58+
const min = this.max * 1.5, max = this.max * 3, sieve = new Array(max + 1).fill(true);
6459
sieve[0] = sieve[1] = false;
6560
for (let i = 2; i <= Math.sqrt(max); i++)
6661
if (sieve[i]) for (let j = i * i; j <= max; j += i) sieve[j] = false;
@@ -69,12 +64,10 @@ class MyHashSet {
6964
}
7065

7166
_rehash() {
72-
this.max *= 2;
67+
this.max *= this._nextPrime();
7368
const oldSet = this.set;
7469
this.set = new Array(this.max).fill(null);
75-
oldSet.forEach((node) => {
76-
while (node) this.add(node.value), (node = node.next);
77-
});
70+
for (let node of oldSet) while (node) this.add(node.value), (node = node.next);
7871
}
7972
}
8073

leetcode_30_days_js/02_counter.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
*/
55
const createCounter = (n) => {
66
let counter = n;
7-
return function () {
8-
return counter++;
9-
};
7+
return () => counter++;
108
};
119

12-
/**
13-
* const counter = createCounter(10)
14-
* counter() // 10
15-
* counter() // 11
16-
* counter() // 12
17-
*/
10+
const counter = createCounter(10);
11+
console.log(counter()); // 10
12+
console.log(counter()); // 11
13+
console.log(counter()); // 12
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} val
3+
* @return {Object}
4+
*/
5+
const expect = (val) => {
6+
return {
7+
toBe: (newVal) => {
8+
if (newVal !== val) throw new Error('Not Equal');
9+
return true;
10+
},
11+
notToBe: (newVal) => {
12+
if (newVal === val) throw new Error('Equal');
13+
return true;
14+
},
15+
};
16+
};
17+
18+
console.log(expect(5).toBe(5)); // true
19+
console.log(expect(5).notToBe(5)); // Error: Equal

leetcode_30_days_js/03_counter-2.js renamed to leetcode_30_days_js/04_counter-2.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ var createCounter = function (init) {
1111
};
1212
};
1313

14-
/**
15-
* const counter = createCounter(5)
16-
* counter.increment(); // 6
17-
* counter.reset(); // 5
18-
* counter.decrement(); // 4
19-
*/
14+
const counter = createCounter(5);
15+
console.log(counter.increment()); // 6
16+
console.log(counter.increment()); // 7
17+
console.log(counter.increment()); // 8

leetcode_30_days_js/04_for-each.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number[]} arr
3+
* @param {Function} fn
4+
* @return {number[]}
5+
*/
6+
const map = (arr, fn) => {
7+
const res = [];
8+
for (let i = 0; i < arr.length; i++) res.push(fn(arr[i], i));
9+
return res;
10+
};
11+
12+
const test = map([1, 2, 3, 4, 5], (v) => v * 2);
13+
console.log(test);

leetcode_30_days_js/07_function-composition.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)