Skip to content

Instantly share code, notes, and snippets.

@fanzhang312
fanzhang312 / removeVowel.py
Created December 12, 2014 20:38
RemoveVowels
def remove(text):
for letter in text:
if letter.lower() in ['a','e','i','o','u']:
text = text.replace(letter, '')
return text
import re
def anti_vowels(text):
@fanzhang312
fanzhang312 / topKElements.java
Created December 6, 2014 00:18
Find the K largest elements in an unsorted array
public static int[] topK(int[] arr, int k) {
// default is ascending order
Queue<Integer> queue = new PriorityQueue<Integer>();
int i = 0;
for (; i < k; i++) {
queue.add(arr[i]);
}
for (; i < arr.length; i++) {
queue.add(arr[i]);
queue.poll(); // Retrieves and removes the head of this queue, which is smallest element in the queue
@fanzhang312
fanzhang312 / minPathSum.java
Last active August 30, 2020 04:45
Find the minimum path sum for binary tree (From root to leaf)
// Find the minimum path sum (from root to leaf)
public static int minPathSum(TreeNode root) {
if(root == null) return 0;
int sum = root.val;
int leftSum = minPathSum(root.left);
int rightSum = minPathSum(root.right);
if(leftSum < rightSum){
sum += leftSum;
@fanzhang312
fanzhang312 / isFibo.java
Created December 4, 2014 05:14
Check if a number is a Fibonacci number
public static boolean isFibo(long n){
return isPerfectSquare(5*n*n+4) || isPerfectSquare(5*n*n-4);
}
public static boolean isPerfectSquare(double a){
int s = (int) Math.sqrt(a);
return s*s == a;
}
@fanzhang312
fanzhang312 / PreorderValidBST.java
Created December 3, 2014 22:22
Preorder Traversal BST
public static boolean validBST(int[] arr){
int lowerbound = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<Integer>();
for(int i : arr){
if(i < lowerbound) return false;
while(!stack.isEmpty() && i > stack.peek()){
lowerbound = stack.pop();
}
stack.push(i);