Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
dmnugent80 / CreateBalancedBST.java
Created February 22, 2015 19:28
Create Balanced BST from sorted array
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@dmnugent80
dmnugent80 / LinkedListRemoveDuplicates.java
Last active December 16, 2016 19:26
Remove Duplicates from Linked List
head->1->2->3->1->4->NULL
head->1->2->3->4->NULL
Given a reference to the head of a linked list, write a function that removes duplicates.
/**
* Definition for singly-linked list.
@dmnugent80
dmnugent80 / MaximunSumPathBinaryTree.java
Created February 25, 2015 20:46
Binary Tree Maximum Sum Path
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@dmnugent80
dmnugent80 / PermutationsIntegerArray.java
Last active August 29, 2015 14:16
Permutations of Integer Array, return List of ArrayLists
public class Solution {
public List<List<Integer>> permute(int[] num) {
boolean[] used = new boolean[num.length];
for (int i = 0; i < used.length; i ++) used[i] = false;
List<List<Integer>> output = new ArrayList<List<Integer>>();
ArrayList<Integer> temp = new ArrayList<Integer>();
permuteHelper(num, 0, used, output, temp);
@dmnugent80
dmnugent80 / MergeSortedArray.java
Created February 25, 2015 21:36
Merge Sorted Array
/*
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
*/
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int indexInsert = m + n - 1;
@dmnugent80
dmnugent80 / BinaryTreeLevelOrderZigZag.java
Created February 26, 2015 03:03
Binary Tree Level Order Zig-Zag Traversal
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@dmnugent80
dmnugent80 / SquareRoot.java
Created February 26, 2015 03:28
Calculate Square Root
public class Solution {
public int sqrt(int x) {
long low = 1;
long high = x/2;
if (x == 0){
return 0;
}
if (x < 4){
return 1;
}
@dmnugent80
dmnugent80 / CompareVersionNumbers.java
Created February 26, 2015 19:14
Compare Version Numbers
public class Solution {
public int compareVersion(String version1, String version2) {
int retval = 0;
String[] strArray1 = version1.split("[.]");
String[] strArray2 = version2.split("[.]");
for (int i = 0; i < Math.max(strArray1.length, strArray2.length); i++){
int val1 = (i < strArray1.length ? Integer.parseInt(strArray1[i]) : 0);
@dmnugent80
dmnugent80 / MinStack.java
Created February 26, 2015 20:24
Min Stack
class MinStack {
Stack<Integer> stk= new Stack<Integer>();
Stack<Integer> min= new Stack<Integer>();
public void push(int x) {
if (min.empty()){
min.push(x);
}
else{
@dmnugent80
dmnugent80 / ConvertLinkedListToBalancedBST.java
Created February 26, 2015 20:46
Convert Linked List to Balanced Binary Search Tree
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
/**
* Definition for binary tree