File tree 2 files changed +66
-1
lines changed
2 files changed +66
-1
lines changed Original file line number Diff line number Diff line change 1
1
class NumArray {
2
-
2
+
3
3
prefixSums : number [ ] = [ ] ;
4
4
5
5
constructor ( nums : number [ ] ) {
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments