Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions 1206.design-skiplist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#
# @lc app=leetcode id=1206 lang=java
#
# [1206] Design Skiplist
#
# @lc code=start
import java.util.Random;

class Skiplist {
static final int MAX_LEVEL = 16;
static final double P = 0.5;
final Node head = new Node(-1, MAX_LEVEL);
int level = 1;
Random rand = new Random();

static class Node {
int val;
Node[] forward;
Node(int val, int level) {
this.val = val;
this.forward = new Node[level];
}
}

public Skiplist() {}

private int randomLevel() {
int lvl = 1;
while (lvl < MAX_LEVEL && rand.nextDouble() < P) {
lvl++;
}
return lvl;
}

public boolean search(int target) {
Node curr = head;
for (int i = level - 1; i >= 0; --i) {
while (curr.forward[i] != null && curr.forward[i].val < target) {
curr = curr.forward[i];
}
}
curr = curr.forward[0];
return curr != null && curr.val == target;
}

public void add(int num) {
Node[] update = new Node[MAX_LEVEL];
Node curr = head;
for (int i = level - 1; i >= 0; --i) {
while (curr.forward[i] != null && curr.forward[i].val < num) {
curr = curr.forward[i];
}
update[i] = curr;
}
int lvl = randomLevel();
if (lvl > level) {
for (int i = level; i < lvl; ++i) {
update[i] = head;
}
level = lvl;
}
Node newNode = new Node(num, lvl);
for (int i = 0; i < lvl; ++i) {
newNode.forward[i] = update[i].forward[i];
update[i].forward[i] = newNode;
}
}

public boolean erase(int num) {
Node[] update = new Node[MAX_LEVEL];
Node curr = head;
for (int i = level - 1; i >= 0; --i) {
while (curr.forward[i] != null && curr.forward[i].val < num) {
curr = curr.forward[i];
}
update[i] = curr;
}
curr = curr.forward[0];
if (curr == null || curr.val != num) return false;
for (int i = 0; i < level; ++i) {
if (update[i].forward[i] != curr) break;
update[i].forward[i] = curr.forward[i];
}
while (level > 1 && head.forward[level - 1] == null) {
level--;
}
return true;
}
}

/**
* Your Skiplist object will be instantiated and called as such:
* Skiplist obj = new Skiplist();
* boolean param_1 = obj.search(target);
* obj.add(num);
* boolean param_3 = obj.erase(num);
*/
# @lc code=end
45 changes: 45 additions & 0 deletions 1367.linked-list-in-binary-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# @lc app=leetcode id=1367 lang=java
#
# [1367] Linked List in Binary Tree
#
# @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubPath(ListNode head, TreeNode root) {
if (root == null) return false;
if (checkPath(head, root)) return true;
return isSubPath(head, root.left) || isSubPath(head, root.right);
}
private boolean checkPath(ListNode head, TreeNode node) {
if (head == null) return true;
if (node == null) return false;
if (head.val != node.val) return false;
return checkPath(head.next, node.left) || checkPath(head.next, node.right);
}
}
# @lc code=end