Skip to content

Instantly share code, notes, and snippets.

@jporcelli
jporcelli / select.py
Last active August 29, 2015 13:56
Selection from an unsorted array in O(n) time
#__author__ James Porcelli
"""
Insertion sort, O(n) for n = 5
"""
def insert_sort(s):
for i in range(1, len(s)):
j = i
while j > 0 and s[j - 1] > s[j]:
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}
int temp = x;
//element in position[0] is first digit
List<Integer> digits = new ArrayList<Integer>();
@jporcelli
jporcelli / addTwoNumbersLinkedList.java
Last active August 29, 2015 14:04
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null;
ListNode traveler = null;
int remainder = 0;
while(l1 != null || l2 != null){
int x = l1.val + l2.val + remainder;
@jporcelli
jporcelli / heightBalancedBinaryTree.java
Created July 20, 2014 07:33
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
public boolean isBalanced(TreeNode root) {
int left, right;
if(root == null){
return true;
}
if(root.left != null){
if(isBalanced(root.left)){
public String addBinary(String a, String b) {
a = new StringBuilder(a).reverse().toString();
b = new StringBuilder(b).reverse().toString();
StringBuffer buf = new StringBuffer(100);
int size = 0;
int remainder = 0;
int i = getVal(a, size);
int j = getVal(b, size);
@jporcelli
jporcelli / BalancedBinaryTreeNoModifications.java
Created August 12, 2014 05:14
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
public boolean isBalanced(TreeNode root) {
if(getHeight(root) == -1){
return false;
}else{
return true;
}
}
public TreeNode sortedArrayToBST(int[] num) {
return buildTree(num, 0, num.length);
}
public TreeNode buildTree(int[] num, int l, int h){
int mid = ((h - l) / 2) + l;
TreeNode root = null;
if(h > l && mid >= 0 && mid < num.length){
@jporcelli
jporcelli / BinaryTreeLevelOrderTraversal.java
Created August 13, 2014 05:00
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
public List<List<Integer>> levelOrder(TreeNode root) {
// Tree maps keep a strict ordering of the values wtr to the order in which
// there inserted, necessary for keeping the level lists in order
Map<Integer, List<Integer>> treeList = new TreeMap<Integer, List<Integer>>();
int level = 0;
if(root != null){
List<Integer> rootLevelList = new ArrayList<Integer>(1);
rootLevelList.add(root.val);
@jporcelli
jporcelli / IterativeInOrderTraversal.c
Last active August 29, 2015 14:05
An iterative in-order binary tree traversal solution I came up with
#include <stdio.h>
/* Stack ops */
void push(void *);
void *pop();
int isEmpty();
void * peak();
void processNode(TreeNode *);
#include <stdio.h>
/* Stack ops */
void push(void *);
void *pop();
int isEmpty();
void * peak();
void processNode(TreeNode *);