This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FactorialSum | |
{ | |
public static void main(String[] args) | |
{ | |
Factorial myObject = new Factorial(); | |
long fact = myObject.factorial(10); | |
System.out.print("factorial: " + fact); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |