Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
dmnugent80 / FactorialTailRecursion.java
Created February 10, 2015 23:51
Factorial Tail Recursion Accumulator
public class FactorialSum
{
public static void main(String[] args)
{
Factorial myObject = new Factorial();
long fact = myObject.factorial(10);
System.out.print("factorial: " + fact);
}
@dmnugent80
dmnugent80 / PowerDigitSum.java
Last active August 29, 2015 14:15
Power Digit Sum
import java.math.BigInteger; // header stuff MUST go above the first class
// our main class becomes a file but the main method is still found
public class PowerDigitSum
{
public static void main(String[] args)
{
BigInteger bigPow = pow(2, 1000);
System.out.println("power: " + bigPow);
@dmnugent80
dmnugent80 / ReturnDeepestNode.java
Last active April 24, 2019 05:23
Return Deepest Node in Binary Tree
//Return deepest, rightmost node in a tree
//Implementation: use DFS
import java.util.Queue;
import java.util.LinkedList;
public class Main
{
public static void main(String[] args)
{
@dmnugent80
dmnugent80 / PrintEachLevelBST.java
Last active August 29, 2015 14:15
Print Each Level BST
//print each tree level on it's own line
//Implementation: use BFS
//Used a BST here, but the function to print each level could be used on any Binary Tree
import java.util.Queue;
import java.util.LinkedList;
public class Main
{
@dmnugent80
dmnugent80 / ReversePolishEval.java
Created February 15, 2015 00:51
Reverse Polish Notation Evaluation
import java.util.Stack;
public class HelloWorld
{
public static void main(String[] args)
{
String[] arr = {"4", "13", "5", "/", "+"};
Eval eval = new Eval();
int n = eval.evalRPN(arr);
@dmnugent80
dmnugent80 / LinkedListIntersection.java
Created February 15, 2015 01:32
Linked List Get Intersection
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@dmnugent80
dmnugent80 / MaximumProductSubarray.java
Last active August 29, 2015 14:15
Maximum Product Subarray
//Most Simple
public class Solution {
public int maxProduct(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
int max = A[0], min = A[0], result = A[0];
for (int i = 1; i < A.length; i++) {
@dmnugent80
dmnugent80 / ReverseSentenceNoPunctuation.java
Last active August 29, 2015 14:15
Reverse Sentence Simple
//In-Place
public class ReverseSentenceInPlace
{
public static void main(String[] args)
{
String str = "Hello, I would, like, to.....,... reverse! This, sentence.,,";
String reversed = reverseSentence(str);
System.out.print(reversed);
@dmnugent80
dmnugent80 / IsSentencePalindrome.java
Last active August 29, 2015 14:15
Is Sentence Palindrome
//In Place algorithm
//A man, a plan, a canal: Panama
public class Solution {
public static void main(String[] args){
String str = "A,,, man, a;; plan, a c....anal: Panama";
boolean retVal = isPalindrome(str);
@dmnugent80
dmnugent80 / FindLinkedListCyclePoint.java
Created February 15, 2015 19:49
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }