You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1200-1299/1206.Design Skiplist/README_EN.md
+111-1
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,23 @@ skiplist.search(1); // return False, 1 has already been erased.</pre>
77
77
78
78
<!-- solution:start -->
79
79
80
-
### Solution 1
80
+
### Solution 1: Data Structure
81
+
82
+
The core idea of a skip list is to use multiple "levels" to store data, with each level acting as an index. Data starts from the bottom level linked list and gradually rises to higher levels, eventually forming a multi-level linked list structure. Each level's nodes only contain part of the data, allowing for jumps to reduce search time.
83
+
84
+
In this problem, we use a $\textit{Node}$ class to represent the nodes of the skip list. Each node contains a $\textit{val}$ field and a $\textit{next}$ array. The length of the array is $\textit{level}$, indicating the next node at each level. We use a $\textit{Skiplist}$ class to implement the skip list operations.
85
+
86
+
The skip list contains a head node $\textit{head}$ and the current maximum level $\textit{level}$. The head node's value is set to $-1$ to mark the starting position of the list. We use a dynamic array $\textit{next}$ to store pointers to successor nodes.
87
+
88
+
For the $\textit{search}$ operation, we start from the highest level of the skip list and traverse downwards until we find the target node or determine that the target node does not exist. At each level, we use the $\textit{find\_closest}$ method to jump to the node closest to the target.
89
+
90
+
For the $\textit{add}$ operation, we first randomly decide the level of the new node. Then, starting from the highest level, we find the node closest to the new value at each level and insert the new node at the appropriate position. If the level of the inserted node is greater than the current maximum level of the skip list, we need to update the level of the skip list.
91
+
92
+
For the $\textit{erase}$ operation, similar to the search operation, we traverse each level of the skip list to find and delete the target node. When deleting a node, we need to update the $\textit{next}$ pointers at each level. If the highest level of the skip list has no nodes, we need to decrease the level of the skip list.
93
+
94
+
Additionally, we define a $\textit{random\_level}$ method to randomly decide the level of the new node. This method generates a random number between $[1, \textit{max\_level}]$ until the generated random number is greater than or equal to $\textit{p}$. We also have a $\textit{find\_closest}$ method to find the node closest to the target value at each level.
95
+
96
+
The time complexity of the above operations is $O(\log n)$, where $n$ is the number of nodes in the skip list. The space complexity is $O(n)$.
81
97
82
98
<!-- tabs:start -->
83
99
@@ -421,6 +437,100 @@ func randomLevel() int {
421
437
*/
422
438
```
423
439
440
+
#### TypeScript
441
+
442
+
```ts
443
+
classNode {
444
+
val:number;
445
+
next: (Node|null)[];
446
+
447
+
constructor(val:number, level:number) {
448
+
this.val=val;
449
+
this.next=Array(level).fill(null);
450
+
}
451
+
}
452
+
453
+
classSkiplist {
454
+
privatestatic maxLevel:number=32;
455
+
privatestatic p:number=0.25;
456
+
private head:Node;
457
+
private level:number;
458
+
459
+
constructor() {
460
+
this.head=newNode(-1, Skiplist.maxLevel);
461
+
this.level=0;
462
+
}
463
+
464
+
search(target:number):boolean {
465
+
let curr =this.head;
466
+
for (let i =this.level-1; i>=0; i--) {
467
+
curr=this.findClosest(curr, i, target);
468
+
if (curr.next[i] &&curr.next[i]!.val===target) {
469
+
returntrue;
470
+
}
471
+
}
472
+
returnfalse;
473
+
}
474
+
475
+
add(num:number):void {
476
+
let curr =this.head;
477
+
const level =this.randomLevel();
478
+
const node =newNode(num, level);
479
+
this.level=Math.max(this.level, level);
480
+
481
+
for (let i =this.level-1; i>=0; i--) {
482
+
curr=this.findClosest(curr, i, num);
483
+
if (i<level) {
484
+
node.next[i] =curr.next[i];
485
+
curr.next[i] =node;
486
+
}
487
+
}
488
+
}
489
+
490
+
erase(num:number):boolean {
491
+
let curr =this.head;
492
+
let ok =false;
493
+
494
+
for (let i =this.level-1; i>=0; i--) {
495
+
curr=this.findClosest(curr, i, num);
496
+
if (curr.next[i] &&curr.next[i]!.val===num) {
497
+
curr.next[i] =curr.next[i]!.next[i];
498
+
ok=true;
499
+
}
500
+
}
501
+
502
+
while (this.level>1&&this.head.next[this.level-1] ===null) {
0 commit comments