Skip to content

Instantly share code, notes, and snippets.

View raymondino's full-sized avatar
keep up all the good work!

Rui Yan raymondino

keep up all the good work!
View GitHub Profile
@raymondino
raymondino / cc4.4
Created August 2, 2017 23:57
check if a binary tree is a binary search tree
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* some tree nodes */
checkBST(n, Integer.min_value, Integer.max_value);
}
public static boolean checkBST(TreeNode n, int min, int max) {
@raymondino
raymondino / cc4.3
Created August 2, 2017 23:25
construct a minimum height binary search tree out of a sorted array
import java.util.*;
class TreeNode{
public int value;
public TreeNode left;
public TreeNode right;
public TreeNode(int v) {
value = v;
left = null;
@raymondino
raymondino / cc4.1
Created August 2, 2017 21:37
check a balanced tree
import java.util.*;
public class Solution {
public static void main(String[] args) {
TreeNode a = new TreeNode(1);
a.left = new TreeNode(2);
a.right = new TreeNode(3);
a.left.left = new TreeNode(4);
a.left.right = new TreeNode(5);
@raymondino
raymondino / cc3.6
Created August 2, 2017 20:38
stack sort
import java.util.*;
public class Solution {
public static void main(String[] args) {
LinkedList<Integer> s = new LinkedList<Integer>();
s.push(4);
s.push(8);
s.push(3);
s.push(5);
@raymondino
raymondino / cc3.4
Created August 2, 2017 20:07
tower of hanoi
class Tower {
private LinkedList<Integer> plates;
public Tower() {
plates = new LinkedList<Integer>();
}
/* add plates */
public void add(int i) {
if(!disks.isEmpty() && disks.peek() <= d) {
@raymondino
raymondino / cc3.1
Created August 2, 2017 19:20
use an array to simulate three stacks
import java.util.*;
public class Solution {
public static void main(String[] args) {
TripleStack ts = new TripleStack(3);
print(ts.pop(0));
print(ts.pop(1));
print(ts.pop(2));
ts.push(0, 1);
@raymondino
raymondino / cc3.7
Created August 2, 2017 17:46
a shelter for dogs and cats
import java.util.*;
public class Solution {
public static void main(String[] args) {
Shelter s = new Shelter();
s.enqueue("dog1");
s.enqueue("dog2");
s.enqueue("cat1");
s.enqueue("dog3");
s.enqueue("cat2");
@raymondino
raymondino / cc3.5
Created August 2, 2017 17:06
implement queue with two stacks
import java.util.*;
public class Solution {
public static void main(String[] args) {
MyQueue q = new MyQueue();
print(q.isEmpty());
print(q.size());
q.offer(1);
q.offer(2);
print(q.isEmpty());
@raymondino
raymondino / cc3.3
Created August 1, 2017 22:46
design a SetOfStacks class
class SetOfStacks {
private static int threshold = 3;
private LinkedList<LinkedList<Integer>> data;
public SetOfStacks() {
data = new LinkedList<LinkedList<Integer>> ();
LinkedList<Integer> stack1 = new LinkedList<Integer>();
data.push(stack1);
}
@raymondino
raymondino / cc3.2
Created August 1, 2017 21:42
design min() functionality in O(1) for stack
class Stack {
private LinkedList<Integer> data;
private LinkedList<Integer> min;
public Integer peek() {
/* return the top */
}
public Integer pop() {
/* pop the top value */