diff --git a/Code Practice/.classpath b/Code Practice/.classpath
deleted file mode 100644
index 28e2b79..0000000
--- a/Code Practice/.classpath
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
-
-
-
-
diff --git a/Code Practice/.gitignore b/Code Practice/.gitignore
deleted file mode 100644
index 5e56e04..0000000
--- a/Code Practice/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/bin
diff --git a/Code Practice/.project b/Code Practice/.project
deleted file mode 100644
index 71f5873..0000000
--- a/Code Practice/.project
+++ /dev/null
@@ -1,17 +0,0 @@
-
-
- my-java-code-samples
-
-
-
-
-
- org.eclipse.jdt.core.javabuilder
-
-
-
-
-
- org.eclipse.jdt.core.javanature
-
-
diff --git a/Code Practice/.settings/org.eclipse.jdt.core.prefs b/Code Practice/.settings/org.eclipse.jdt.core.prefs
deleted file mode 100644
index 416f4fb..0000000
--- a/Code Practice/.settings/org.eclipse.jdt.core.prefs
+++ /dev/null
@@ -1,11 +0,0 @@
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
-org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
-org.eclipse.jdt.core.compiler.compliance=1.5
-org.eclipse.jdt.core.compiler.debug.lineNumber=generate
-org.eclipse.jdt.core.compiler.debug.localVariable=generate
-org.eclipse.jdt.core.compiler.debug.sourceFile=generate
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.5
diff --git a/Code Practice/src/solutions/LinkedList.java b/Code Practice/src/solutions/LinkedList.java
deleted file mode 100644
index 9fc7fd2..0000000
--- a/Code Practice/src/solutions/LinkedList.java
+++ /dev/null
@@ -1,89 +0,0 @@
-package solutions;
-
-import structures.LLNode;
-
-public class LinkedList {
-
- //ctci:P2.1
- public static LLNode removeDuplicates(LLNode head){
- LLNode n = head;
- int ndata;
- while (n.getNext() != null) {
- ndata=n.getData();
- n.setNext(LLNode.deleteNode(n.getNext(),ndata));
- n = n.getNext();
- if (n == null){return head;}
- }
- return head;
- }
-
-
- //ctci:P2.2
- public static LLNode findNthLastElement(LLNode head, int nth){
- LLNode n = head;
- int nf=0;
- while (n.getNext() != null) {
- n = n.getNext();
- nf++;
- }
-
- nth=nf-nth;
- if (nth <= 0){return head;}
- n = head;
- for (int i=0; i
+ 4.0.0
+ com.example
+ my-java-code-samples
+ 1.0-SNAPSHOT
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
+
+
diff --git a/Code Practice/src/solutions/ArraysnStrings.java b/src/main/java/solutions/ArraysnStrings.java
similarity index 70%
rename from Code Practice/src/solutions/ArraysnStrings.java
rename to src/main/java/solutions/ArraysnStrings.java
index 4b4ff60..e1feff1 100644
--- a/Code Practice/src/solutions/ArraysnStrings.java
+++ b/src/main/java/solutions/ArraysnStrings.java
@@ -9,58 +9,62 @@
public class ArraysnStrings {
-
- //ctci:P1.1
- public static Boolean isUnique(String input){
- Set seenCharacters = new HashSet();
- for (char ch: input.toCharArray()){
- if (seenCharacters.contains(ch)){
- return false;
- }
- seenCharacters.add(ch);
- }
- return true;
- }
-
- //ctci:P1.1
- public static Boolean isUniqueWOAddStructs(String input){
- int slen = input.length();
- for (int i=0; i < slen-1; i++ ){
- for (int j=i+1; j < slen; j++){
- if (input.charAt(i) == input.charAt(j)){
- return false;
- }
- }
- }
- return true;
- }
-
- //ctci:P1.2
- public static String reverseString(String input){
- char[] inputCharA = input.toCharArray();
- int length = input.length();
- char[] outputCharA = new char[length];
- int j;
- for (int i= 0; i < length; i++){
- j=length-1-i;
- outputCharA[i]=inputCharA[j];
- }
- return String.valueOf(outputCharA);
- }
-
- //helper removeDuplicates
- static void moveOneBack(char[] str, int i){
- char cc=str[i];
- while (cc != '\0'){
- str[i]=str[i+1];
- i=i+1;
- cc=str[i];
- }
- }
-
- //ctci:P1.3
- public static void removeDuplicates(char[] str) {
- int i=0;
+
+ //ctci:P1.1
+ public static Boolean isUnique(String input){
+ if(input == null || input.isEmpty()){ return true;}
+
+ Set seenCharacters = new HashSet();
+ for (char ch: input.toCharArray()){
+ if (seenCharacters.contains(ch)){
+ return false;
+ }
+ seenCharacters.add(ch);
+ }
+ return true;
+ }
+
+ //ctci:P1.1
+ public static Boolean isUniqueWOAddStructs(String input){
+ if(input == null || input.isEmpty()){ return true;}
+
+ int slen = input.length();
+ for (int i=0; i < slen-1; i++ ){
+ for (int j=i+1; j < slen; j++){
+ if (input.charAt(i) == input.charAt(j)){
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ //ctci:P1.2
+ public static String reverseString(String input){
+ char[] inputCharA = input.toCharArray();
+ int length = input.length();
+ char[] outputCharA = new char[length];
+ int j;
+ for (int i= 0; i < length; i++){
+ j=length-1-i;
+ outputCharA[i]=inputCharA[j];
+ }
+ return String.valueOf(outputCharA);
+ }
+
+ //helper removeDuplicates
+ static void moveOneBack(char[] str, int i){
+ char cc=str[i];
+ while (cc != '\0'){
+ str[i]=str[i+1];
+ i=i+1;
+ cc=str[i];
+ }
+ }
+
+ //ctci:P1.3
+ public static void removeDuplicates(char[] str) {
+ int i=0;
int j;
while(str[i]!= '\0'){
j=i+1;
@@ -74,32 +78,34 @@ public static void removeDuplicates(char[] str) {
i++;
}
}
-
- //ctci:P1.4
- public static boolean isAnagram(String str1, String str2){
- Map let2char = new HashMap(); //letter to character
- //counts letters in str1
- for (char c: str1.toCharArray()){
+ //ctci:P1.4
+ public static boolean isAnagram(String str1, String str2){
+ Map let2char = new HashMap(); //letter to character
+
+ // counts letters in str1
+ for (char c: str1.toCharArray()){
if ( let2char.containsKey(c)){
let2char.put(c, let2char.get(c) + 1);
- }
- else{
+ }else{
let2char.put(c, 1);
}
}
- int ccount;
+ int ccount;
for (char c: str2.toCharArray()){
if ( let2char.containsKey(c)){
ccount=let2char.get(c);
- if (ccount -1 == 0){let2char.remove(c);}
- else {let2char.put(c, ccount-1);}
- }
- else{ return false;}
- }
- if (let2char.size() == 0){ return true;}
- return false;
+ if (ccount -1 == 0){
+ let2char.remove(c);
+ }else {
+ let2char.put(c, ccount-1);
+ }
+ }else{
+ return false;
+ }
+ }
+ return let2char.isEmpty();
}
//leetcode Palindrome Pairs
@@ -218,5 +224,4 @@ private LinkedList> allPalindromesForEmpty(String[] words, int myi
}
return sol;
}
-
}
diff --git a/Code Practice/src/solutions/BitManipulation.java b/src/main/java/solutions/BitManipulation.java
similarity index 100%
rename from Code Practice/src/solutions/BitManipulation.java
rename to src/main/java/solutions/BitManipulation.java
diff --git a/Code Practice/src/solutions/HackerRank.java b/src/main/java/solutions/HackerRank.java
similarity index 100%
rename from Code Practice/src/solutions/HackerRank.java
rename to src/main/java/solutions/HackerRank.java
diff --git a/src/main/java/solutions/LinkedList.java b/src/main/java/solutions/LinkedList.java
new file mode 100644
index 0000000..29996b4
--- /dev/null
+++ b/src/main/java/solutions/LinkedList.java
@@ -0,0 +1,88 @@
+package solutions;
+
+import structures.LinkedListNode;
+
+public class LinkedList {
+
+ //ctci:P2.1
+ public static LinkedListNode removeDuplicates(LinkedListNode head){
+ LinkedListNode n = head;
+ int ndata;
+ while (n.next != null) {
+ ndata=n.data;
+ n.next=LinkedListNode.deleteNode(n.next,ndata);
+ n = n.next;
+ if (n == null){return head;}
+ }
+ return head;
+ }
+
+ //ctci:P2.2
+ public static LinkedListNode findNthLastElement(LinkedListNode head, int nth){
+ LinkedListNode n = head;
+ int nf=0;
+ while (n.next != null) {
+ n = n.next;
+ nf++;
+ }
+
+ nth=nf-nth;
+ if (nth <= 0){return head;}
+ n = head;
+ for (int i=0; i= 0;
+ }
+
+ private static int isBalancedRecursive(BinaryTreeNode root) {
+ if(root == null) {
+ return 0;
+ }
+
+ if(root.left == null && root.right == null) { //leaf
+ return 1;
+ }
+
+ if(root.left == null) {
+ if(isBalancedRecursive(root.right) == 1) { //right is leaf
+ return 2;
+ }
+ return -1;
+ }
+
+ if(root.right == null) {
+ if(isBalancedRecursive(root.left) == 1) { //left is leaf
+ return 2;
+ }
+ return -1;
+ }
+ int left = isBalancedRecursive(root.left);
+ int right = isBalancedRecursive(root.right);
+ if(left == -1 || right == -1 || Integer.max(left-right, right-left)>1) {
+ return -1;
+ }
+ return 1+Integer.max(left, right);
+ }
+
+ //ctci:P4.3
+ public static BinaryTreeNode makeTree(int[] num){
+
+ if(num == null || num.length == 0){
+ return null;
+ }
+
+ return makeTreeRecursive(num, 0, num.length);
+ }
+
+ //s inclusive, e exclusive
+ private static BinaryTreeNode makeTreeRecursive(int[] num, int s, int e){
+ if(e-s == 0){
+ return null;
+ }else if (e-s == 1){
+ return new BinaryTreeNode(num[s]);
+ }
+
+ BinaryTreeNode r;
+
+ if(e-s == 2){
+ if(random.nextInt(2)==1){
+ r=new BinaryTreeNode(num[s]);
+ r.right=new BinaryTreeNode(num[s+1]);
+ }else{
+ r=new BinaryTreeNode(num[s+1]);
+ r.left=new BinaryTreeNode(num[s]);
+ }
+ }else{
+ int mid=((e-s)/2)+s;
+ r=new BinaryTreeNode(num[mid]);
+ r.left=makeTreeRecursive(num, s, mid);
+ r.right=makeTreeRecursive(num, mid+1, e);
+ }
+
+ return r;
+
+ }
+
+ //ctci:4.4
+ public static LinkedListNode[] linkedListPerDepth(BinaryTreeNode bst){
+ if(bst == null) {
+ return new LinkedListNode[0];
+ }
+ ArrayList arr=new ArrayList();
+ arr = linkedListPerDepthRecursive(bst, arr, 1);
+
+ LinkedListNode[] sol = new LinkedListNode[arr.size()];
+ return arr.toArray(sol);
+ }
+
+ private static ArrayList linkedListPerDepthRecursive(
+ BinaryTreeNode bst, ArrayList arr, int d){
+ if (bst == null) {
+ return arr;
+ }
+
+ if(arr.size() < d) {
+ arr.add(new LinkedListNode(bst.val));
+ }else{
+ arr.get(d-1).appendToTail(bst.val);
+ }
+
+ if(bst.left != null) {
+ arr = linkedListPerDepthRecursive(bst.left, arr, d+1);
+ }
+
+ if(bst.right != null) {
+ arr = linkedListPerDepthRecursive(bst.right, arr, d+1);
+ }
+
+ return arr;
+ }
+
+ // p4.6 Design an algorithm and write code to find the first common ancestor of two nodes
+ // in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not
+ // necessarily a binary search tree.
+
+ //preorder-traversal (left and right first) compare lists
+
+
+}
diff --git a/src/main/java/structures/BinaryTreeNode.java b/src/main/java/structures/BinaryTreeNode.java
new file mode 100644
index 0000000..6e8f58e
--- /dev/null
+++ b/src/main/java/structures/BinaryTreeNode.java
@@ -0,0 +1,48 @@
+package structures;
+
+import java.util.List;
+
+public class BinaryTreeNode {
+ public int val;
+ public Object data;
+ public BinaryTreeNode left;
+ public BinaryTreeNode right;
+
+ public BinaryTreeNode(int x){
+ val=x;
+ left=null;
+ right=null;
+ }
+
+ public BinaryTreeNode(int x, Object input){
+ val=x;
+ left=null;
+ right=null;
+ data=input;
+ }
+
+ public BinaryTreeNode(int index, Object input, int size, List