This file contains 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
//Used this example as a template: http://www.algolist.net/Data_structures/Hash_table/Simple_example | |
/* | |
*HashEntry class | |
*/ | |
public class HashEntry { | |
public HashEntry next; | |
private int key; | |
private int value; |
This file contains 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
/* Balanced delimiter problem | |
Opening: (, [, { | |
Closing: ), ], } | |
Valid: {}()[] | |
([{}]) | |
Invalid: ([)] | |
*/ |
This file contains 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
/* | |
Interview Question: | |
Here's a binary tree: find the longest path within it. | |
So, find a path between any two leaf nodes, where the path is the longest.” | |
*/ | |
public class TreeNode{ | |
int data; | |
TreeNode left; | |
TreeNode right; |
This file contains 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
/* | |
Interview Question: | |
Here's a binary tree: find the longest path within it. | |
So, find a path between any two leaf nodes, where the path is the longest.” | |
*/ | |
public class TreeNode{ | |
int data; | |
TreeNode left; | |
TreeNode right; |
This file contains 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
/* | |
Interview Question: | |
Here's a binary tree: find the longest path within it. | |
So, find a path between any two leaf nodes, where the path is the longest.” | |
*/ | |
public class TreeNode{ | |
int data; | |
TreeNode left; | |
TreeNode right; |
This file contains 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
//Delete duplicates in an array | |
public static int[] deleteDuplicates( int[] arrayWithDups ){ | |
ArrayList<Integer> arrayNoDups= new ArrayList<Integer>(); | |
for (int i=0; i < arrayWithDups.length; i++){ | |
if (!arrayNoDups.contains(arrayWithDups[i])){ | |
arrayNoDups.add(arrayWithDups[i]); | |
} | |
} | |
int[] arrayPrimitive = new int[arrayNoDups.size()]; |
This file contains 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
//Breadth First Search, traverse tree and print it out level-by-level | |
public class TreeNode{ | |
public int data; | |
public int level; | |
public TreeNode left; | |
public TreeNode right; | |
public TreeNode(int d, int l){ | |
this.data = d; | |
this.level = l; |
This file contains 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 static int pow(int x, int n){ | |
int mid; | |
int temp; | |
if (n == 0){ | |
return 1; | |
} | |
if (n == 1){ | |
return x; | |
} | |
This file contains 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 TreeNode{ | |
public int data; | |
public TreeNode left; | |
public TreeNode right; | |
public TreeNode(int d){ | |
this.data = d; | |
this.left = null; | |
this.right = null; | |
} | |
} |
This file contains 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 int getCountDuplicates(int[] array){ | |
HashSet<Integer> set = new HashSet<Integer>(); | |
int dupCount = 0; | |
for (int i=0; i<array.length; i++){ | |
if (!set.add(array[i]){ | |
dupCount++; | |
} | |
} | |
return dupCount; | |
} |
OlderNewer