Skip to content

Commit

Permalink
bounded priority blocking queue
Browse files Browse the repository at this point in the history
  • Loading branch information
lichaojacobs committed Dec 19, 2019
1 parent 590ef45 commit 566fac6
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
*/
public class TreeNode {

public int val;
public TreeNode next;
public TreeNode left;
public TreeNode right;
public TreeNode parent;
public int val;
public TreeNode next;
public TreeNode left;
public TreeNode right;
public TreeNode parent;

public TreeNode(int data) {
this.val = data;
}
public TreeNode(int data) {
this.val = data;
}

@Override
public String toString() {
return "TreeNode{" +
"val=" + val +
", next=" + next +
", left=" + left +
", right=" + right +
", parent=" + parent +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.jacobs.basic.algorithm.leetcode;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -25,6 +28,66 @@ public static void main(String[] args) {
// root.right.right = new TreeNode(6);

flatten(root);
String str = " @ @";
String[] arr = str.split("@");
System.out.println(arr);

System.out.println(8 >> 8);

byte[] bytes = new byte[]{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

System.out.println(toKey(100, (byte) '+'));

System.out.println(0xF);

root.left = new TreeNode(2);

Object test = (Object) root;

System.out.println(test.toString());

}

private static final byte[] HEX_BYTES = new byte[]{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
private static final byte POSITIVE_MARKER = (byte) '=';
private static final byte NEGATIVE_MARKER = (byte) '*';

static byte[] toKey(Object value, byte prefix) {
final byte[] result;
int bytes;

if (value instanceof Integer) {
bytes = Integer.SIZE;
} else if (value instanceof Long) {
bytes = Long.SIZE;
} else if (value instanceof Short) {
bytes = Short.SIZE;
} else if (value instanceof Byte) {
bytes = Byte.SIZE;
} else {
throw new IllegalArgumentException(String.format("Type %s not allowed as key.",
value.getClass().getName()));
}

bytes = bytes / Byte.SIZE;

byte[] key = new byte[bytes + 2];
long longValue = ((Number) value).longValue();
key[0] = prefix;
key[1] = longValue >= 0 ? POSITIVE_MARKER : NEGATIVE_MARKER;

for (int i = 0; i < key.length - 2; i++) {
int masked = (int) ((longValue >>> (4 * i)) & 0xF);
key[key.length - i - 1] = HEX_BYTES[masked];
}

result = key;

return result;
}

/**
Expand Down
Loading

0 comments on commit 566fac6

Please sign in to comment.