Skip to content

Instantly share code, notes, and snippets.

View shixiaoyu's full-sized avatar
🤪
Why so Serious?

Xiaoyu shixiaoyu

🤪
Why so Serious?
View GitHub Profile
private double curMinDelta = Double.MAX_VALUE;
private int smallestValue = 0;
public int closestValue(TreeNode root, double target) {
this.searchMin(root, target);
return this.smallestValue;
}
private void searchMin(TreeNode root, double target) {
if (root == null) {
public List<Integer> closestKValuesUsingPreOrder(TreeNode root, double target, int k) {
List<Integer> res = new ArrayList<>();
// PQ by default is ascending order using the element's natural order, so by default, it's a min heap
Queue<Node> pq = new PriorityQueue<>(); // max heap
this.preOrderTraversal(root, target, pq, k);
for (Node n : pq) {
res.add(n.val);
}
/**
this method is also worst case O(N), however, if BST is balanced, this would be O(K + lgN)
*/
public List<Integer> closestKValues(TreeNode root, double target, int k) {
List<Integer> res = new ArrayList<>();
Stack<Integer> predecessors = new Stack<>();
Stack<Integer> successors = new Stack<>();
this.traverseTreeInorder(root, target, predecessors);
private final int[] xDirection = {1, 0, -1, 0};
private final int[] yDirection = {0, -1, 0, 1};
private final int ONE_MILLION = 1000000;
public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
if (blocked == null || source == null || target == null) {
return false;
}
Set<String> blockLookup = this.indexBlockedMatrixToSet(blocked);
private final int[] xDirection = {1, 0, -1, 0};
private final int[] yDirection = {0, -1, 0, 1};
private final int ONE_MILLION = 1000000;
private final int MAX_COUNT_THRESHOLD = 20000;
public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
if (blocked == null || source == null || target == null) {
return false;
}
// I recommend this solution: just need map to keep the mapping relationship
public boolean wordPattern(String pattern, String str) {
if (pattern == null || pattern.length() == 0 || str == null || str.length() == 0) {
return false;
}
String[] strs = str.trim().split(" ");
if (pattern.length() != strs.length) {
return false;
// Reference: https://leetcode.com/problems/word-pattern/discuss/73402/8-lines-simple-Java
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length())
return false;
Map index = new HashMap();
for (Integer i=0; i<words.length; ++i)
if (index.put(pattern.charAt(i), i) != index.put(words[i], i))
return false;
return true;
// This way will also work, just a little bit more work by encoding each string into the same one
// Kinda similar to the isomorphic string
public boolean wordPatternEncoding(String pattern, String str) {
if (str == null || str.isEmpty() || pattern == null || pattern.isEmpty()) {
return false;
}
String[] s = str.split(" ");
if (pattern.length() != s.length) {
return false;
public boolean isIsomorphic(String a, String b) {
if (a == null || b == null || a.length() != b.length()) {
return false;
}
Map<Character, Character> lookup = new HashMap<>();
Set<Character> dupSet = new HashSet<>();
for (int i = 0; i < a.length(); i++) {
char c1 = a.charAt(i);
char c2 = b.charAt(i);
public boolean wordPatternMatch(String pattern, String str) {
if (pattern == null || str == null) {
return false;
}
Map<Character, String> lookup = new HashMap<>();
Set<String> dup = new HashSet<>();
return this.isMatch(pattern, str, lookup, dup);
}