Skip to content

Commit 1f0ea7f

Browse files
authored
Merge pull request #3103 from D00NIK/main
Create 0303-range-sum-query-immutable.ts, 0705-design-hashset.ts
2 parents 44e7863 + 006677f commit 1f0ea7f

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Diff for: typescript/0303-range-sum-query-immutable.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class NumArray {
2-
2+
33
prefixSums: number[] = [];
44

55
constructor(nums: number[]) {

Diff for: typescript/0705-design-hashset.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class _ListNode { // ListNode has a confict
2+
key: number;
3+
next: _ListNode | undefined;
4+
5+
constructor(key: number) {
6+
this.key = key;
7+
}
8+
}
9+
10+
class MyHashSet {
11+
readonly ARRAY_LENGTH = Math.pow(10, 4);
12+
set = new Array<_ListNode>(this.ARRAY_LENGTH);
13+
14+
constructor() {
15+
for (let i = 0; i < this.ARRAY_LENGTH; i++)
16+
this.set[i] = new _ListNode(0);
17+
}
18+
19+
add(key: number): void {
20+
let cur = this.set[key % this.set.length];
21+
22+
while (cur.next) {
23+
if (cur.next.key === key)
24+
return;
25+
26+
cur = cur.next;
27+
}
28+
29+
cur.next = new _ListNode(key);
30+
}
31+
32+
remove(key: number): void {
33+
let cur = this.set[key % this.set.length];
34+
35+
while (cur.next) {
36+
if (cur.next.key === key) {
37+
cur.next = cur.next.next;
38+
return;
39+
}
40+
41+
cur = cur.next;
42+
}
43+
}
44+
45+
contains(key: number): boolean {
46+
let cur = this.set[key % this.set.length];
47+
48+
while (cur.next) {
49+
if (cur.next.key === key)
50+
return true;
51+
52+
cur = cur.next;
53+
}
54+
55+
return false;
56+
}
57+
}
58+
59+
/**
60+
* Your MyHashSet object will be instantiated and called as such:
61+
* var obj = new MyHashSet()
62+
* obj.add(key)
63+
* obj.remove(key)
64+
* var param_3 = obj.contains(key)
65+
*/

0 commit comments

Comments
 (0)