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
// Below is the online solution that keeps a running sum, no extra space is needed other than recursion stack
private int accumulatingSum = 0;
public TreeNode convertBST(TreeNode root) {
// Reset the accumulating sum
this.accumulatingSum = 0;
this.reverseInorderTraversal(root);
return root;
}
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
public void push(int x) {
stack.add(x);
if (minStack.isEmpty()) {
minStack.add(x);
} else {
// Here I am only adding the element if it is <= min, another way is always add minium
class MinStackNode {
public int val;
public int curMin;
public MinStackNode(int val, int curMin) {
this.val = val;
this.curMin = curMin;
}
}
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res = new ArrayList<>();
if (strs == null || strs.length == 0) {
return res;
}
Map<String, List<String>> lookup = new HashMap<>();
for (String s : strs) {
class Solution {
// good practice to define constant
public static final int MAX = 256;
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<>();
if (p == null || s == null || s.length() == 0 || p.length() > s.length()) {
return res;
}
@shixiaoyu
shixiaoyu / ValidPalindromeIIV1.java
Last active March 24, 2019 16:25
Valid Palindrome II version 1
// A quick implementation, a bit duplicate on checking on the palindrome part
public boolean validPalindrome(String s) {
// It says s is non-empty, the other is covered by normal case, so don't need this check
if (s == null || s.length() <= 2) {
return true;
}
int i = 0;
int j = s.length() - 1;
@shixiaoyu
shixiaoyu / ValidPalindromeIIV2.java
Created March 24, 2019 16:25
Valid Palindrome II version 2
// Need an intermediate Result class to return multiple values from a function
public class Result{
public boolean isPalindrome = false;
public int diffStartIndex = 0;
public int diffEndIndex = 0;
public Result(boolean isPalindrome, int diffStartIndex, int diffEndIndex) {
this.isPalindrome = isPalindrome;
this.diffStartIndex = diffStartIndex;
this.diffEndIndex = diffEndIndex;
public boolean isMatchTLE(String s, String p) {
if (s == null || p == null) {
return false;
}
return isMatchHelper(s, 0, p, 0);
}
// b*b*ab**ba*b**b***bba , this should trim some of the recursion time
public String removeDuplicateStars(String p) {
// This is the DP solution, and falls perfectly using the DP template
// Also this is only boolean answer (not find all steps) || find the extreme values (min,max)
public boolean isMatch(String s, String p) {
if (s == null || p == null) {
return false;
}
int row = s.length();
int col = p.length();
boolean[][] lookup = new boolean[row + 1][col + 1];
public boolean isMatch(String s, String p) {
if (s == null || p == null) {
return false;
}
int row = s.length();
int col = p.length();
boolean[][] lookup = new boolean[row + 1][col + 1];