Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
dmnugent80 / RemoveDuplicatesArray.java
Created February 28, 2015 04:10
Remove Duplicates from Sorted Array
public class Solution {
public int removeDuplicates(int[] A) {
if (A.length == 0) return 0;
int a = A[0];
int count = 0;
for (int i = 1; i < A.length; i++){
if (a == A[i]){
count++;
}
@dmnugent80
dmnugent80 / GetMinDepthBinaryTree.java
Created February 28, 2015 19:32
Get Min Depth of Binary Tree from Root
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@dmnugent80
dmnugent80 / MergeTwoSortedLinkedLists.java
Created February 28, 2015 20:06
Merge Two Sorted Linked Lists
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@dmnugent80
dmnugent80 / IterativeBinaryTreePreOrder.java
Created February 28, 2015 22:57
Iterative Binary Tree Pre-order Traversal
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@dmnugent80
dmnugent80 / IterativeBinaryTreePostOrder.java
Last active August 29, 2015 14:16
Iterative Binary Tree Post-order Traversal
//Given a binary tree, return the postorder traversal of its nodes' values.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
@dmnugent80
dmnugent80 / RotateArrayInPlace.java
Created March 1, 2015 00:43
Rotate Array In-Place
public class Solution {
public void rotate(int[] nums, int k) {
if (k == 0) return;
k = k % nums.length;
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length-1);
@dmnugent80
dmnugent80 / InsertAndMergeInterval.java
Last active August 29, 2015 14:16
Insert and Merge Interval
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
@dmnugent80
dmnugent80 / RotateLinkedList.java
Last active August 29, 2015 14:16
Rotate Linked List
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@dmnugent80
dmnugent80 / BuyAndSellStockTwoTransactions.java
Created March 1, 2015 18:37
Best Time to Buy and Sell Stock, max two transactions
public class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 0) return 0;
int[] rearProfits = new int[prices.length];
int maxPrice = prices[prices.length-1];
int maxProfit = 0;
int price = 0;
for (int i = prices.length-2; i >= 0; i--){
price = prices[i];
@dmnugent80
dmnugent80 / IsSameBinaryTree.java
Created March 1, 2015 19:01
Is Same Binary Tree
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {