diff --git a/.DS_Store b/.DS_Store
index 23a63a5..c842db5 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/Algorithm-CPP/.DS_Store b/Algorithm-CPP/.DS_Store
new file mode 100644
index 0000000..7121468
Binary files /dev/null and b/Algorithm-CPP/.DS_Store differ
diff --git a/Algorithm-CPP/Algorithm-C/collobert11a.xcworkspace/contents.xcworkspacedata b/Algorithm-CPP/Algorithm-C/collobert11a.xcworkspace/contents.xcworkspacedata
new file mode 100644
index 0000000..88b0762
--- /dev/null
+++ b/Algorithm-CPP/Algorithm-C/collobert11a.xcworkspace/contents.xcworkspacedata
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
diff --git a/Algorithm-CPP/Algorithm-C/collobert11a.xcworkspace/xcuserdata/sanqiangzhao.xcuserdatad/UserInterfaceState.xcuserstate b/Algorithm-CPP/Algorithm-C/collobert11a.xcworkspace/xcuserdata/sanqiangzhao.xcuserdatad/UserInterfaceState.xcuserstate
new file mode 100644
index 0000000..3b21a39
Binary files /dev/null and b/Algorithm-CPP/Algorithm-C/collobert11a.xcworkspace/xcuserdata/sanqiangzhao.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/Algorithm-CPP/Algorithm-C/word-search.c b/Algorithm-CPP/Algorithm-C/word-search.c
new file mode 100644
index 0000000..fc8a83a
--- /dev/null
+++ b/Algorithm-CPP/Algorithm-C/word-search.c
@@ -0,0 +1,36 @@
+//
+// word-search.c
+//
+//
+// Created by Sanqiang Zhao on 11/25/15.
+//
+//
+
+#include "word-search.h"
+bool existRecursive(char** board, int boardRowSize, int boardColSize, char* word, int idx, int nword,int row, int col){
+ if (board[row][col] == word[idx]) {
+ char temp =board[row][col];
+ board[row][col] = '*';
+ if (idx == nword-1) {
+ return true;
+ }else if((row +1 < boardRowSize && board[row+1][col]!='*' && existRecursive(board,boardRowSize,boardColSize,word,idx+1,nword,row+1,col)) ||(row -1 >=0 && board[row-1][col]!='*' && existRecursive(board,boardRowSize,boardColSize,word,idx+1,nword,row-1,col)) ||(col +1 < boardColSize && board[row][col+1]!='*' && existRecursive(board,boardRowSize,boardColSize,word,idx+1,nword,row,col+1)) ||(col -1 >=0 && board[row][col-1]!='*' && existRecursive(board,boardRowSize,boardColSize,word,idx+1,nword,row,col-1))){
+ return true;
+ }
+ board[row][col] =temp;
+ }
+ return false;
+}
+
+
+bool exist(char** board, int boardRowSize, int boardColSize, char* word) {
+ unsigned long nword = strlen(word);
+
+ for (int row = 0; row < boardRowSize; ++row) {
+ for (int col = 0; col < boardColSize ; ++col) {
+ if(word[0] == board[row][col] && existRecursive(board,boardRowSize,boardColSize,word,0,nword,row,col)){
+ return true;
+ }
+ }
+ }
+ return false;
+}
\ No newline at end of file
diff --git a/Algorithm-CPP/Algorithm-C/word-search.h b/Algorithm-CPP/Algorithm-C/word-search.h
new file mode 100644
index 0000000..7ae7bfa
--- /dev/null
+++ b/Algorithm-CPP/Algorithm-C/word-search.h
@@ -0,0 +1,14 @@
+//
+// word-search.h
+//
+//
+// Created by Sanqiang Zhao on 11/25/15.
+//
+//
+
+#ifndef word_search_h
+#define word_search_h
+
+#include
+
+#endif /* word_search_h */
diff --git a/Algorithm-Linux.xcodeproj/project.pbxproj b/Algorithm-CPP/Algorithm-Linux.xcodeproj/project.pbxproj
similarity index 100%
rename from Algorithm-Linux.xcodeproj/project.pbxproj
rename to Algorithm-CPP/Algorithm-Linux.xcodeproj/project.pbxproj
diff --git a/Algorithm-Linux.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Algorithm-CPP/Algorithm-Linux.xcodeproj/project.xcworkspace/contents.xcworkspacedata
similarity index 100%
rename from Algorithm-Linux.xcodeproj/project.xcworkspace/contents.xcworkspacedata
rename to Algorithm-CPP/Algorithm-Linux.xcodeproj/project.xcworkspace/contents.xcworkspacedata
diff --git a/Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/UserInterfaceState.xcuserstate b/Algorithm-CPP/Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/UserInterfaceState.xcuserstate
similarity index 100%
rename from Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/UserInterfaceState.xcuserstate
rename to Algorithm-CPP/Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/UserInterfaceState.xcuserstate
diff --git a/Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Expressions.xcexplist b/Algorithm-CPP/Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Expressions.xcexplist
similarity index 100%
rename from Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Expressions.xcexplist
rename to Algorithm-CPP/Algorithm-Linux.xcodeproj/project.xcworkspace/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Expressions.xcexplist
diff --git a/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/Algorithm-CPP/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
similarity index 100%
rename from Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
rename to Algorithm-CPP/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
diff --git a/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/Algorithm-Linux.xcscheme b/Algorithm-CPP/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/Algorithm-Linux.xcscheme
similarity index 100%
rename from Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/Algorithm-Linux.xcscheme
rename to Algorithm-CPP/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/Algorithm-Linux.xcscheme
diff --git a/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/xcschememanagement.plist b/Algorithm-CPP/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/xcschememanagement.plist
similarity index 100%
rename from Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/xcschememanagement.plist
rename to Algorithm-CPP/Algorithm-Linux.xcodeproj/xcuserdata/zhaosanqiang916.xcuserdatad/xcschemes/xcschememanagement.plist
diff --git a/Entity.cpp b/Algorithm-CPP/Entity.cpp
similarity index 100%
rename from Entity.cpp
rename to Algorithm-CPP/Entity.cpp
diff --git a/Entity.h b/Algorithm-CPP/Entity.h
similarity index 100%
rename from Entity.h
rename to Algorithm-CPP/Entity.h
diff --git a/KMP.cpp b/Algorithm-CPP/KMP.cpp
similarity index 100%
rename from KMP.cpp
rename to Algorithm-CPP/KMP.cpp
diff --git a/KMP.h b/Algorithm-CPP/KMP.h
similarity index 100%
rename from KMP.h
rename to Algorithm-CPP/KMP.h
diff --git a/Q11-VerifyUniqueCharInString.cpp b/Algorithm-CPP/Q11-VerifyUniqueCharInString.cpp
similarity index 100%
rename from Q11-VerifyUniqueCharInString.cpp
rename to Algorithm-CPP/Q11-VerifyUniqueCharInString.cpp
diff --git a/Q11-VerifyUniqueCharInString.h b/Algorithm-CPP/Q11-VerifyUniqueCharInString.h
similarity index 100%
rename from Q11-VerifyUniqueCharInString.h
rename to Algorithm-CPP/Q11-VerifyUniqueCharInString.h
diff --git a/Q111-MergeSortedArray.cpp b/Algorithm-CPP/Q111-MergeSortedArray.cpp
similarity index 100%
rename from Q111-MergeSortedArray.cpp
rename to Algorithm-CPP/Q111-MergeSortedArray.cpp
diff --git a/Q111-MergeSortedArray.h b/Algorithm-CPP/Q111-MergeSortedArray.h
similarity index 100%
rename from Q111-MergeSortedArray.h
rename to Algorithm-CPP/Q111-MergeSortedArray.h
diff --git a/Q112-SortString.cpp b/Algorithm-CPP/Q112-SortString.cpp
similarity index 100%
rename from Q112-SortString.cpp
rename to Algorithm-CPP/Q112-SortString.cpp
diff --git a/Q112-SortString.h b/Algorithm-CPP/Q112-SortString.h
similarity index 100%
rename from Q112-SortString.h
rename to Algorithm-CPP/Q112-SortString.h
diff --git a/Q113-FindInRotatedArr.cpp b/Algorithm-CPP/Q113-FindInRotatedArr.cpp
similarity index 100%
rename from Q113-FindInRotatedArr.cpp
rename to Algorithm-CPP/Q113-FindInRotatedArr.cpp
diff --git a/Q113-FindInRotatedArr.h b/Algorithm-CPP/Q113-FindInRotatedArr.h
similarity index 100%
rename from Q113-FindInRotatedArr.h
rename to Algorithm-CPP/Q113-FindInRotatedArr.h
diff --git a/Q115-FindStringInIntersperse.cpp b/Algorithm-CPP/Q115-FindStringInIntersperse.cpp
similarity index 100%
rename from Q115-FindStringInIntersperse.cpp
rename to Algorithm-CPP/Q115-FindStringInIntersperse.cpp
diff --git a/Q115-FindStringInIntersperse.h b/Algorithm-CPP/Q115-FindStringInIntersperse.h
similarity index 100%
rename from Q115-FindStringInIntersperse.h
rename to Algorithm-CPP/Q115-FindStringInIntersperse.h
diff --git a/Q116-SearchOnMatrix.cpp b/Algorithm-CPP/Q116-SearchOnMatrix.cpp
similarity index 100%
rename from Q116-SearchOnMatrix.cpp
rename to Algorithm-CPP/Q116-SearchOnMatrix.cpp
diff --git a/Q116-SearchOnMatrix.h b/Algorithm-CPP/Q116-SearchOnMatrix.h
similarity index 100%
rename from Q116-SearchOnMatrix.h
rename to Algorithm-CPP/Q116-SearchOnMatrix.h
diff --git a/Q117-LongestIncreasingSequence.cpp b/Algorithm-CPP/Q117-LongestIncreasingSequence.cpp
similarity index 100%
rename from Q117-LongestIncreasingSequence.cpp
rename to Algorithm-CPP/Q117-LongestIncreasingSequence.cpp
diff --git a/Q117-LongestIncreasingSequence.h b/Algorithm-CPP/Q117-LongestIncreasingSequence.h
similarity index 100%
rename from Q117-LongestIncreasingSequence.h
rename to Algorithm-CPP/Q117-LongestIncreasingSequence.h
diff --git a/Q118-TrackBinarySearchTree.cpp b/Algorithm-CPP/Q118-TrackBinarySearchTree.cpp
similarity index 100%
rename from Q118-TrackBinarySearchTree.cpp
rename to Algorithm-CPP/Q118-TrackBinarySearchTree.cpp
diff --git a/Q118-TrackBinarySearchTree.h b/Algorithm-CPP/Q118-TrackBinarySearchTree.h
similarity index 100%
rename from Q118-TrackBinarySearchTree.h
rename to Algorithm-CPP/Q118-TrackBinarySearchTree.h
diff --git a/Q12-ReverseString.cpp b/Algorithm-CPP/Q12-ReverseString.cpp
similarity index 100%
rename from Q12-ReverseString.cpp
rename to Algorithm-CPP/Q12-ReverseString.cpp
diff --git a/Q12-ReverseString.h b/Algorithm-CPP/Q12-ReverseString.h
similarity index 100%
rename from Q12-ReverseString.h
rename to Algorithm-CPP/Q12-ReverseString.h
diff --git a/Q13-VerifyAnagram.cpp b/Algorithm-CPP/Q13-VerifyAnagram.cpp
similarity index 100%
rename from Q13-VerifyAnagram.cpp
rename to Algorithm-CPP/Q13-VerifyAnagram.cpp
diff --git a/Q13-VerifyAnagram.h b/Algorithm-CPP/Q13-VerifyAnagram.h
similarity index 100%
rename from Q13-VerifyAnagram.h
rename to Algorithm-CPP/Q13-VerifyAnagram.h
diff --git a/Q14-encodeUrl.cpp b/Algorithm-CPP/Q14-encodeUrl.cpp
similarity index 100%
rename from Q14-encodeUrl.cpp
rename to Algorithm-CPP/Q14-encodeUrl.cpp
diff --git a/Q14-encodeUrl.h b/Algorithm-CPP/Q14-encodeUrl.h
similarity index 100%
rename from Q14-encodeUrl.h
rename to Algorithm-CPP/Q14-encodeUrl.h
diff --git a/Q15-compressStr.cpp b/Algorithm-CPP/Q15-compressStr.cpp
similarity index 100%
rename from Q15-compressStr.cpp
rename to Algorithm-CPP/Q15-compressStr.cpp
diff --git a/Q15-compressStr.h b/Algorithm-CPP/Q15-compressStr.h
similarity index 100%
rename from Q15-compressStr.h
rename to Algorithm-CPP/Q15-compressStr.h
diff --git a/Q16-rotateMatrix.cpp b/Algorithm-CPP/Q16-rotateMatrix.cpp
similarity index 100%
rename from Q16-rotateMatrix.cpp
rename to Algorithm-CPP/Q16-rotateMatrix.cpp
diff --git a/Q16-rotateMatrix.h b/Algorithm-CPP/Q16-rotateMatrix.h
similarity index 100%
rename from Q16-rotateMatrix.h
rename to Algorithm-CPP/Q16-rotateMatrix.h
diff --git a/Q17-SetZeroOnMatrix.cpp b/Algorithm-CPP/Q17-SetZeroOnMatrix.cpp
similarity index 100%
rename from Q17-SetZeroOnMatrix.cpp
rename to Algorithm-CPP/Q17-SetZeroOnMatrix.cpp
diff --git a/Q17-SetZeroOnMatrix.h b/Algorithm-CPP/Q17-SetZeroOnMatrix.h
similarity index 100%
rename from Q17-SetZeroOnMatrix.h
rename to Algorithm-CPP/Q17-SetZeroOnMatrix.h
diff --git a/Q18-VerifyRotation.cpp b/Algorithm-CPP/Q18-VerifyRotation.cpp
similarity index 100%
rename from Q18-VerifyRotation.cpp
rename to Algorithm-CPP/Q18-VerifyRotation.cpp
diff --git a/Q18-VerifyRotation.h b/Algorithm-CPP/Q18-VerifyRotation.h
similarity index 100%
rename from Q18-VerifyRotation.h
rename to Algorithm-CPP/Q18-VerifyRotation.h
diff --git a/Q2-ReverseString.cpp b/Algorithm-CPP/Q2-ReverseString.cpp
similarity index 100%
rename from Q2-ReverseString.cpp
rename to Algorithm-CPP/Q2-ReverseString.cpp
diff --git a/Q2-ReverseString.h b/Algorithm-CPP/Q2-ReverseString.h
similarity index 100%
rename from Q2-ReverseString.h
rename to Algorithm-CPP/Q2-ReverseString.h
diff --git a/Q21-removeDuplacate.cpp b/Algorithm-CPP/Q21-removeDuplacate.cpp
similarity index 100%
rename from Q21-removeDuplacate.cpp
rename to Algorithm-CPP/Q21-removeDuplacate.cpp
diff --git a/Q21-removeDuplacate.h b/Algorithm-CPP/Q21-removeDuplacate.h
similarity index 100%
rename from Q21-removeDuplacate.h
rename to Algorithm-CPP/Q21-removeDuplacate.h
diff --git a/Q22-findLastNthElement.cpp b/Algorithm-CPP/Q22-findLastNthElement.cpp
similarity index 100%
rename from Q22-findLastNthElement.cpp
rename to Algorithm-CPP/Q22-findLastNthElement.cpp
diff --git a/Q22-findLastNthElement.h b/Algorithm-CPP/Q22-findLastNthElement.h
similarity index 100%
rename from Q22-findLastNthElement.h
rename to Algorithm-CPP/Q22-findLastNthElement.h
diff --git a/Q23-deleteElement.cpp b/Algorithm-CPP/Q23-deleteElement.cpp
similarity index 100%
rename from Q23-deleteElement.cpp
rename to Algorithm-CPP/Q23-deleteElement.cpp
diff --git a/Q23-deleteElement.h b/Algorithm-CPP/Q23-deleteElement.h
similarity index 100%
rename from Q23-deleteElement.h
rename to Algorithm-CPP/Q23-deleteElement.h
diff --git a/Q24-PartitionLinkedList.cpp b/Algorithm-CPP/Q24-PartitionLinkedList.cpp
similarity index 100%
rename from Q24-PartitionLinkedList.cpp
rename to Algorithm-CPP/Q24-PartitionLinkedList.cpp
diff --git a/Q24-PartitionLinkedList.h b/Algorithm-CPP/Q24-PartitionLinkedList.h
similarity index 100%
rename from Q24-PartitionLinkedList.h
rename to Algorithm-CPP/Q24-PartitionLinkedList.h
diff --git a/Q25-AddLinkedList.cpp b/Algorithm-CPP/Q25-AddLinkedList.cpp
similarity index 100%
rename from Q25-AddLinkedList.cpp
rename to Algorithm-CPP/Q25-AddLinkedList.cpp
diff --git a/Q25-AddLinkedList.h b/Algorithm-CPP/Q25-AddLinkedList.h
similarity index 100%
rename from Q25-AddLinkedList.h
rename to Algorithm-CPP/Q25-AddLinkedList.h
diff --git a/Q26-findEntryInCircularList.cpp b/Algorithm-CPP/Q26-findEntryInCircularList.cpp
similarity index 100%
rename from Q26-findEntryInCircularList.cpp
rename to Algorithm-CPP/Q26-findEntryInCircularList.cpp
diff --git a/Q26-findEntryInCircularList.h b/Algorithm-CPP/Q26-findEntryInCircularList.h
similarity index 100%
rename from Q26-findEntryInCircularList.h
rename to Algorithm-CPP/Q26-findEntryInCircularList.h
diff --git a/Q27-VerifyPalindrome.cpp b/Algorithm-CPP/Q27-VerifyPalindrome.cpp
similarity index 100%
rename from Q27-VerifyPalindrome.cpp
rename to Algorithm-CPP/Q27-VerifyPalindrome.cpp
diff --git a/Q27-VerifyPalindrome.h b/Algorithm-CPP/Q27-VerifyPalindrome.h
similarity index 100%
rename from Q27-VerifyPalindrome.h
rename to Algorithm-CPP/Q27-VerifyPalindrome.h
diff --git a/Q3-VerifyAnagram.cpp b/Algorithm-CPP/Q3-VerifyAnagram.cpp
similarity index 100%
rename from Q3-VerifyAnagram.cpp
rename to Algorithm-CPP/Q3-VerifyAnagram.cpp
diff --git a/Q3-VerifyAnagram.h b/Algorithm-CPP/Q3-VerifyAnagram.h
similarity index 100%
rename from Q3-VerifyAnagram.h
rename to Algorithm-CPP/Q3-VerifyAnagram.h
diff --git a/Q31-ThreeStacksFromOneArray.cpp b/Algorithm-CPP/Q31-ThreeStacksFromOneArray.cpp
similarity index 100%
rename from Q31-ThreeStacksFromOneArray.cpp
rename to Algorithm-CPP/Q31-ThreeStacksFromOneArray.cpp
diff --git a/Q31-ThreeStacksFromOneArray.h b/Algorithm-CPP/Q31-ThreeStacksFromOneArray.h
similarity index 100%
rename from Q31-ThreeStacksFromOneArray.h
rename to Algorithm-CPP/Q31-ThreeStacksFromOneArray.h
diff --git a/Q32-StackWithMin.cpp b/Algorithm-CPP/Q32-StackWithMin.cpp
similarity index 100%
rename from Q32-StackWithMin.cpp
rename to Algorithm-CPP/Q32-StackWithMin.cpp
diff --git a/Q32-StackWithMin.h b/Algorithm-CPP/Q32-StackWithMin.h
similarity index 100%
rename from Q32-StackWithMin.h
rename to Algorithm-CPP/Q32-StackWithMin.h
diff --git a/Q33-SetOfStacks.cpp b/Algorithm-CPP/Q33-SetOfStacks.cpp
similarity index 100%
rename from Q33-SetOfStacks.cpp
rename to Algorithm-CPP/Q33-SetOfStacks.cpp
diff --git a/Q33-SetOfStacks.h b/Algorithm-CPP/Q33-SetOfStacks.h
similarity index 100%
rename from Q33-SetOfStacks.h
rename to Algorithm-CPP/Q33-SetOfStacks.h
diff --git a/Q34-TowersOfHanoi.cpp b/Algorithm-CPP/Q34-TowersOfHanoi.cpp
similarity index 100%
rename from Q34-TowersOfHanoi.cpp
rename to Algorithm-CPP/Q34-TowersOfHanoi.cpp
diff --git a/Q34-TowersOfHanoi.h b/Algorithm-CPP/Q34-TowersOfHanoi.h
similarity index 100%
rename from Q34-TowersOfHanoi.h
rename to Algorithm-CPP/Q34-TowersOfHanoi.h
diff --git a/Q35-QueueWithTwoStacks.cpp b/Algorithm-CPP/Q35-QueueWithTwoStacks.cpp
similarity index 100%
rename from Q35-QueueWithTwoStacks.cpp
rename to Algorithm-CPP/Q35-QueueWithTwoStacks.cpp
diff --git a/Q35-QueueWithTwoStacks.h b/Algorithm-CPP/Q35-QueueWithTwoStacks.h
similarity index 100%
rename from Q35-QueueWithTwoStacks.h
rename to Algorithm-CPP/Q35-QueueWithTwoStacks.h
diff --git a/Q36-SortStack.cpp b/Algorithm-CPP/Q36-SortStack.cpp
similarity index 100%
rename from Q36-SortStack.cpp
rename to Algorithm-CPP/Q36-SortStack.cpp
diff --git a/Q36-SortStack.h b/Algorithm-CPP/Q36-SortStack.h
similarity index 100%
rename from Q36-SortStack.h
rename to Algorithm-CPP/Q36-SortStack.h
diff --git a/Q37-AnimalShelter.cpp b/Algorithm-CPP/Q37-AnimalShelter.cpp
similarity index 100%
rename from Q37-AnimalShelter.cpp
rename to Algorithm-CPP/Q37-AnimalShelter.cpp
diff --git a/Q37-AnimalShelter.h b/Algorithm-CPP/Q37-AnimalShelter.h
similarity index 100%
rename from Q37-AnimalShelter.h
rename to Algorithm-CPP/Q37-AnimalShelter.h
diff --git a/Q41-CheckBalanceTree.cpp b/Algorithm-CPP/Q41-CheckBalanceTree.cpp
similarity index 100%
rename from Q41-CheckBalanceTree.cpp
rename to Algorithm-CPP/Q41-CheckBalanceTree.cpp
diff --git a/Q41-CheckBalanceTree.h b/Algorithm-CPP/Q41-CheckBalanceTree.h
similarity index 100%
rename from Q41-CheckBalanceTree.h
rename to Algorithm-CPP/Q41-CheckBalanceTree.h
diff --git a/Q42-FindRouteBFS.cpp b/Algorithm-CPP/Q42-FindRouteBFS.cpp
similarity index 100%
rename from Q42-FindRouteBFS.cpp
rename to Algorithm-CPP/Q42-FindRouteBFS.cpp
diff --git a/Q42-FindRouteBFS.h b/Algorithm-CPP/Q42-FindRouteBFS.h
similarity index 100%
rename from Q42-FindRouteBFS.h
rename to Algorithm-CPP/Q42-FindRouteBFS.h
diff --git a/Q43-BinarySearchTreeFromSortedArray.cpp b/Algorithm-CPP/Q43-BinarySearchTreeFromSortedArray.cpp
similarity index 100%
rename from Q43-BinarySearchTreeFromSortedArray.cpp
rename to Algorithm-CPP/Q43-BinarySearchTreeFromSortedArray.cpp
diff --git a/Q43-BinarySearchTreeFromSortedArray.h b/Algorithm-CPP/Q43-BinarySearchTreeFromSortedArray.h
similarity index 100%
rename from Q43-BinarySearchTreeFromSortedArray.h
rename to Algorithm-CPP/Q43-BinarySearchTreeFromSortedArray.h
diff --git a/Q44-BuildLinkedListFromTreeBFS.cpp b/Algorithm-CPP/Q44-BuildLinkedListFromTreeBFS.cpp
similarity index 100%
rename from Q44-BuildLinkedListFromTreeBFS.cpp
rename to Algorithm-CPP/Q44-BuildLinkedListFromTreeBFS.cpp
diff --git a/Q44-BuildLinkedListFromTreeBFS.h b/Algorithm-CPP/Q44-BuildLinkedListFromTreeBFS.h
similarity index 100%
rename from Q44-BuildLinkedListFromTreeBFS.h
rename to Algorithm-CPP/Q44-BuildLinkedListFromTreeBFS.h
diff --git a/Q45-CheckBinarySearchTree.cpp b/Algorithm-CPP/Q45-CheckBinarySearchTree.cpp
similarity index 100%
rename from Q45-CheckBinarySearchTree.cpp
rename to Algorithm-CPP/Q45-CheckBinarySearchTree.cpp
diff --git a/Q45-CheckBinarySearchTree.h b/Algorithm-CPP/Q45-CheckBinarySearchTree.h
similarity index 100%
rename from Q45-CheckBinarySearchTree.h
rename to Algorithm-CPP/Q45-CheckBinarySearchTree.h
diff --git a/Q46-NextOnBinaryTree.cpp b/Algorithm-CPP/Q46-NextOnBinaryTree.cpp
similarity index 100%
rename from Q46-NextOnBinaryTree.cpp
rename to Algorithm-CPP/Q46-NextOnBinaryTree.cpp
diff --git a/Q46-NextOnBinaryTree.h b/Algorithm-CPP/Q46-NextOnBinaryTree.h
similarity index 100%
rename from Q46-NextOnBinaryTree.h
rename to Algorithm-CPP/Q46-NextOnBinaryTree.h
diff --git a/Q47-FindCommonAncester.cpp b/Algorithm-CPP/Q47-FindCommonAncester.cpp
similarity index 100%
rename from Q47-FindCommonAncester.cpp
rename to Algorithm-CPP/Q47-FindCommonAncester.cpp
diff --git a/Q47-FindCommonAncester.h b/Algorithm-CPP/Q47-FindCommonAncester.h
similarity index 100%
rename from Q47-FindCommonAncester.h
rename to Algorithm-CPP/Q47-FindCommonAncester.h
diff --git a/Q48-CheckSubTree.cpp b/Algorithm-CPP/Q48-CheckSubTree.cpp
similarity index 100%
rename from Q48-CheckSubTree.cpp
rename to Algorithm-CPP/Q48-CheckSubTree.cpp
diff --git a/Q48-CheckSubTree.h b/Algorithm-CPP/Q48-CheckSubTree.h
similarity index 100%
rename from Q48-CheckSubTree.h
rename to Algorithm-CPP/Q48-CheckSubTree.h
diff --git a/Q49-FindSumBFS.cpp b/Algorithm-CPP/Q49-FindSumBFS.cpp
similarity index 100%
rename from Q49-FindSumBFS.cpp
rename to Algorithm-CPP/Q49-FindSumBFS.cpp
diff --git a/Q49-FindSumBFS.h b/Algorithm-CPP/Q49-FindSumBFS.h
similarity index 100%
rename from Q49-FindSumBFS.h
rename to Algorithm-CPP/Q49-FindSumBFS.h
diff --git a/Q51-UpdateBitOnMN.cpp b/Algorithm-CPP/Q51-UpdateBitOnMN.cpp
similarity index 100%
rename from Q51-UpdateBitOnMN.cpp
rename to Algorithm-CPP/Q51-UpdateBitOnMN.cpp
diff --git a/Q51-UpdateBitOnMN.h b/Algorithm-CPP/Q51-UpdateBitOnMN.h
similarity index 100%
rename from Q51-UpdateBitOnMN.h
rename to Algorithm-CPP/Q51-UpdateBitOnMN.h
diff --git a/Q52-PrintBinary.cpp b/Algorithm-CPP/Q52-PrintBinary.cpp
similarity index 100%
rename from Q52-PrintBinary.cpp
rename to Algorithm-CPP/Q52-PrintBinary.cpp
diff --git a/Q52-PrintBinary.h b/Algorithm-CPP/Q52-PrintBinary.h
similarity index 100%
rename from Q52-PrintBinary.h
rename to Algorithm-CPP/Q52-PrintBinary.h
diff --git a/Q53-NextNumWithSameBit.cpp b/Algorithm-CPP/Q53-NextNumWithSameBit.cpp
similarity index 100%
rename from Q53-NextNumWithSameBit.cpp
rename to Algorithm-CPP/Q53-NextNumWithSameBit.cpp
diff --git a/Q53-NextNumWithSameBit.h b/Algorithm-CPP/Q53-NextNumWithSameBit.h
similarity index 100%
rename from Q53-NextNumWithSameBit.h
rename to Algorithm-CPP/Q53-NextNumWithSameBit.h
diff --git a/Q55-BitSwapRequired.cpp b/Algorithm-CPP/Q55-BitSwapRequired.cpp
similarity index 100%
rename from Q55-BitSwapRequired.cpp
rename to Algorithm-CPP/Q55-BitSwapRequired.cpp
diff --git a/Q55-BitSwapRequired.h b/Algorithm-CPP/Q55-BitSwapRequired.h
similarity index 100%
rename from Q55-BitSwapRequired.h
rename to Algorithm-CPP/Q55-BitSwapRequired.h
diff --git a/Q56-SwapOddEvenBit.cpp b/Algorithm-CPP/Q56-SwapOddEvenBit.cpp
similarity index 100%
rename from Q56-SwapOddEvenBit.cpp
rename to Algorithm-CPP/Q56-SwapOddEvenBit.cpp
diff --git a/Q56-SwapOddEvenBit.h b/Algorithm-CPP/Q56-SwapOddEvenBit.h
similarity index 100%
rename from Q56-SwapOddEvenBit.h
rename to Algorithm-CPP/Q56-SwapOddEvenBit.h
diff --git a/Q57-FindMissing.cpp b/Algorithm-CPP/Q57-FindMissing.cpp
similarity index 100%
rename from Q57-FindMissing.cpp
rename to Algorithm-CPP/Q57-FindMissing.cpp
diff --git a/Q57-FindMissing.h b/Algorithm-CPP/Q57-FindMissing.h
similarity index 100%
rename from Q57-FindMissing.h
rename to Algorithm-CPP/Q57-FindMissing.h
diff --git a/Q58-DrawHorizontalLine.cpp b/Algorithm-CPP/Q58-DrawHorizontalLine.cpp
similarity index 100%
rename from Q58-DrawHorizontalLine.cpp
rename to Algorithm-CPP/Q58-DrawHorizontalLine.cpp
diff --git a/Q58-DrawHorizontalLine.h b/Algorithm-CPP/Q58-DrawHorizontalLine.h
similarity index 100%
rename from Q58-DrawHorizontalLine.h
rename to Algorithm-CPP/Q58-DrawHorizontalLine.h
diff --git a/Q91-CountWaysOnStairs.cpp b/Algorithm-CPP/Q91-CountWaysOnStairs.cpp
similarity index 100%
rename from Q91-CountWaysOnStairs.cpp
rename to Algorithm-CPP/Q91-CountWaysOnStairs.cpp
diff --git a/Q91-CountWaysOnStairs.h b/Algorithm-CPP/Q91-CountWaysOnStairs.h
similarity index 100%
rename from Q91-CountWaysOnStairs.h
rename to Algorithm-CPP/Q91-CountWaysOnStairs.h
diff --git a/Q910-CreateStackBox.cpp b/Algorithm-CPP/Q910-CreateStackBox.cpp
similarity index 100%
rename from Q910-CreateStackBox.cpp
rename to Algorithm-CPP/Q910-CreateStackBox.cpp
diff --git a/Q910-CreateStackBox.h b/Algorithm-CPP/Q910-CreateStackBox.h
similarity index 100%
rename from Q910-CreateStackBox.h
rename to Algorithm-CPP/Q910-CreateStackBox.h
diff --git a/Q911-BinaryPresentation.cpp b/Algorithm-CPP/Q911-BinaryPresentation.cpp
similarity index 100%
rename from Q911-BinaryPresentation.cpp
rename to Algorithm-CPP/Q911-BinaryPresentation.cpp
diff --git a/Q911-BinaryPresentation.h b/Algorithm-CPP/Q911-BinaryPresentation.h
similarity index 100%
rename from Q911-BinaryPresentation.h
rename to Algorithm-CPP/Q911-BinaryPresentation.h
diff --git a/Q92-RobotMovement.cpp b/Algorithm-CPP/Q92-RobotMovement.cpp
similarity index 100%
rename from Q92-RobotMovement.cpp
rename to Algorithm-CPP/Q92-RobotMovement.cpp
diff --git a/Q92-RobotMovement.h b/Algorithm-CPP/Q92-RobotMovement.h
similarity index 100%
rename from Q92-RobotMovement.h
rename to Algorithm-CPP/Q92-RobotMovement.h
diff --git a/Q93-FindMagicIndex.cpp b/Algorithm-CPP/Q93-FindMagicIndex.cpp
similarity index 100%
rename from Q93-FindMagicIndex.cpp
rename to Algorithm-CPP/Q93-FindMagicIndex.cpp
diff --git a/Q93-FindMagicIndex.h b/Algorithm-CPP/Q93-FindMagicIndex.h
similarity index 100%
rename from Q93-FindMagicIndex.h
rename to Algorithm-CPP/Q93-FindMagicIndex.h
diff --git a/Q94-Subsets.cpp b/Algorithm-CPP/Q94-Subsets.cpp
similarity index 100%
rename from Q94-Subsets.cpp
rename to Algorithm-CPP/Q94-Subsets.cpp
diff --git a/Q94-Subsets.h b/Algorithm-CPP/Q94-Subsets.h
similarity index 100%
rename from Q94-Subsets.h
rename to Algorithm-CPP/Q94-Subsets.h
diff --git a/Q95-Permutation.cpp b/Algorithm-CPP/Q95-Permutation.cpp
similarity index 100%
rename from Q95-Permutation.cpp
rename to Algorithm-CPP/Q95-Permutation.cpp
diff --git a/Q95-Permutation.h b/Algorithm-CPP/Q95-Permutation.h
similarity index 100%
rename from Q95-Permutation.h
rename to Algorithm-CPP/Q95-Permutation.h
diff --git a/Q96-ValidParenthesis.cpp b/Algorithm-CPP/Q96-ValidParenthesis.cpp
similarity index 100%
rename from Q96-ValidParenthesis.cpp
rename to Algorithm-CPP/Q96-ValidParenthesis.cpp
diff --git a/Q96-ValidParenthesis.h b/Algorithm-CPP/Q96-ValidParenthesis.h
similarity index 100%
rename from Q96-ValidParenthesis.h
rename to Algorithm-CPP/Q96-ValidParenthesis.h
diff --git a/Q97-FillColor.cpp b/Algorithm-CPP/Q97-FillColor.cpp
similarity index 100%
rename from Q97-FillColor.cpp
rename to Algorithm-CPP/Q97-FillColor.cpp
diff --git a/Q97-FillColor.h b/Algorithm-CPP/Q97-FillColor.h
similarity index 100%
rename from Q97-FillColor.h
rename to Algorithm-CPP/Q97-FillColor.h
diff --git a/Q98-MakeCoinCharges.cpp b/Algorithm-CPP/Q98-MakeCoinCharges.cpp
similarity index 100%
rename from Q98-MakeCoinCharges.cpp
rename to Algorithm-CPP/Q98-MakeCoinCharges.cpp
diff --git a/Q98-MakeCoinCharges.h b/Algorithm-CPP/Q98-MakeCoinCharges.h
similarity index 100%
rename from Q98-MakeCoinCharges.h
rename to Algorithm-CPP/Q98-MakeCoinCharges.h
diff --git a/Q99-PlaceQueen.cpp b/Algorithm-CPP/Q99-PlaceQueen.cpp
similarity index 100%
rename from Q99-PlaceQueen.cpp
rename to Algorithm-CPP/Q99-PlaceQueen.cpp
diff --git a/Q99-PlaceQueen.h b/Algorithm-CPP/Q99-PlaceQueen.h
similarity index 100%
rename from Q99-PlaceQueen.h
rename to Algorithm-CPP/Q99-PlaceQueen.h
diff --git a/QuickSort.cpp b/Algorithm-CPP/QuickSort.cpp
similarity index 100%
rename from QuickSort.cpp
rename to Algorithm-CPP/QuickSort.cpp
diff --git a/QuickSort.h b/Algorithm-CPP/QuickSort.h
similarity index 100%
rename from QuickSort.h
rename to Algorithm-CPP/QuickSort.h
diff --git a/README.md b/Algorithm-CPP/README.md
similarity index 100%
rename from README.md
rename to Algorithm-CPP/README.md
diff --git a/Util.cpp b/Algorithm-CPP/Util.cpp
similarity index 100%
rename from Util.cpp
rename to Algorithm-CPP/Util.cpp
diff --git a/Util.h b/Algorithm-CPP/Util.h
similarity index 100%
rename from Util.h
rename to Algorithm-CPP/Util.h
diff --git a/Algorithm-Java-Backup/.DS_Store b/Algorithm-Java-Backup/.DS_Store
new file mode 100644
index 0000000..1efdda3
Binary files /dev/null and b/Algorithm-Java-Backup/.DS_Store differ
diff --git a/Algorithm-Java/SpiralMatrix.html b/Algorithm-Java-Backup/SpiralMatrix.html
similarity index 100%
rename from Algorithm-Java/SpiralMatrix.html
rename to Algorithm-Java-Backup/SpiralMatrix.html
diff --git a/Algorithm-Java/build.xml b/Algorithm-Java-Backup/build.xml
similarity index 100%
rename from Algorithm-Java/build.xml
rename to Algorithm-Java-Backup/build.xml
diff --git a/Algorithm-Java/hs_err_pid19383.log b/Algorithm-Java-Backup/hs_err_pid19383.log
similarity index 100%
rename from Algorithm-Java/hs_err_pid19383.log
rename to Algorithm-Java-Backup/hs_err_pid19383.log
diff --git a/Algorithm-Java/manifest.mf b/Algorithm-Java-Backup/manifest.mf
similarity index 100%
rename from Algorithm-Java/manifest.mf
rename to Algorithm-Java-Backup/manifest.mf
diff --git a/Algorithm-Java/nbproject/build-impl.xml b/Algorithm-Java-Backup/nbproject/build-impl.xml
similarity index 100%
rename from Algorithm-Java/nbproject/build-impl.xml
rename to Algorithm-Java-Backup/nbproject/build-impl.xml
diff --git a/Algorithm-Java/nbproject/genfiles.properties b/Algorithm-Java-Backup/nbproject/genfiles.properties
similarity index 100%
rename from Algorithm-Java/nbproject/genfiles.properties
rename to Algorithm-Java-Backup/nbproject/genfiles.properties
diff --git a/Algorithm-Java/nbproject/project.properties b/Algorithm-Java-Backup/nbproject/project.properties
similarity index 100%
rename from Algorithm-Java/nbproject/project.properties
rename to Algorithm-Java-Backup/nbproject/project.properties
diff --git a/Algorithm-Java/nbproject/project.xml b/Algorithm-Java-Backup/nbproject/project.xml
similarity index 100%
rename from Algorithm-Java/nbproject/project.xml
rename to Algorithm-Java-Backup/nbproject/project.xml
diff --git a/Algorithm-Java-Backup/src/.DS_Store b/Algorithm-Java-Backup/src/.DS_Store
new file mode 100644
index 0000000..3f67fe3
Binary files /dev/null and b/Algorithm-Java-Backup/src/.DS_Store differ
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/KMP.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/KMP.java
new file mode 100644
index 0000000..4b23bbd
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/KMP.java
@@ -0,0 +1,53 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.ArraysAndStrings;
+
+public class KMP {
+
+ public int search(String text, String pattern) {
+ int count = 0, length = text.length(), p_length = pattern.length(), i = 0, pos = 0;
+ int[] tab = next(pattern);
+ while (i < length) {
+ if (text.charAt(i) == pattern.charAt(pos)) {
+ ++pos;
+ if (pos == p_length) {
+ ++count;
+ pos = 0;
+ }
+ } else {
+ if (pos > 0) {
+ pos = tab[pos - 1] - 1;
+ }
+ }
+ ++i;
+ }
+ return count;
+ }
+
+ public int[] next(String pattern) {
+ int pos = 0, i = 1, length = pattern.length();
+ int[] tab = new int[length];
+ //tab[0] = -1;
+ while (i < length) {
+ if (pattern.charAt(i) == pattern.charAt(pos)) {
+ tab[i++] = ++pos;
+ } else {
+ if (pos > 0) {
+ --pos;
+ } else {
+ tab[i++] = pos;
+ }
+ }
+ }
+ return tab;
+ }
+
+ public static void main(String[] args) {
+ String pattern = "ababdab";
+ String text = "ababdabssababdab";
+ KMP kmp = new KMP();
+ int count = kmp.search(text, pattern);
+ System.out.println(count);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_1_IsUnique.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_1_IsUnique.java
new file mode 100644
index 0000000..62bea2f
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_1_IsUnique.java
@@ -0,0 +1,46 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ArraysAndStrings;
+
+public class Q1_1_IsUnique {
+
+ boolean isUnique(String text) {
+ int i, length = text.length();
+ if (length > 0xff) {
+ return false;
+ }
+ boolean[] charset = new boolean[0xff];
+ for (i = 0; i < length; i++) {
+ char ch = text.charAt(i);
+ if (charset[ch]) {
+ return false;
+ } else {
+ charset[ch] = true;
+ }
+ }
+ return true;
+ }
+
+ boolean isUnique2(String text) {
+ int i, length = text.length();
+ if (length > 0xff) {
+ return false;
+ }
+ int intchecker = 0;
+ for (i = 0; i < length; i++) {
+ char ch = text.charAt(i);
+ if (((1 << ch) & intchecker) > 0) {
+ return false;
+ } else {
+ intchecker |= (1 << ch);
+ }
+ }
+ return true;
+ }
+
+ public static void main(String[] args) {
+ Q1_1_IsUnique q1 = new Q1_1_IsUnique();
+ System.out.println(q1.isUnique2("abcda"));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_3_IsPermutation.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_3_IsPermutation.java
new file mode 100644
index 0000000..56f6a05
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_3_IsPermutation.java
@@ -0,0 +1,28 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ArraysAndStrings;
+
+public class Q1_3_IsPermutation {
+
+ boolean isPermutation(String text, String pattern) {
+ int i, length = text.length();
+ if (length != pattern.length()) {
+ return false;
+ }
+ short[] charset = new short[0xff];
+ for (i = 0; i < length; i++) {
+ ++charset[text.charAt(i)];
+ }
+ for (i = 0; i < length; i++) {
+ --charset[text.charAt(i)];
+ if (charset[text.charAt(i)] < 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_4_UrlEncode.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_4_UrlEncode.java
new file mode 100644
index 0000000..c408308
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_4_UrlEncode.java
@@ -0,0 +1,37 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ArraysAndStrings;
+
+public class Q1_4_UrlEncode {
+
+ void encode(char[] text, int length) {
+ int i, newlength = length, j;
+ for (i = 0; i < length; i++) {
+ char ch = text[i];
+ if (ch == ' ') {
+ newlength += 2;
+ }
+ }
+ j = newlength - 1;
+ for (i = length - 1; i >= 0; --i) {
+ if (text[i] != ' ') {
+ text[j--] = text[i];
+ } else {
+ text[j--] = '0';
+ text[j--] = '2';
+ text[j--] = '%';
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ char[] text = "abc def ".toCharArray();
+ Q1_4_UrlEncode q1 = new Q1_4_UrlEncode();
+ q1.encode(text, 7);
+ for (char c : text) {
+ System.out.print(c);
+ }
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_5_CompressText.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_5_CompressText.java
new file mode 100644
index 0000000..4012cd9
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_5_CompressText.java
@@ -0,0 +1,49 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ArraysAndStrings;
+
+public class Q1_5_CompressText {
+
+ String compress(String text) {
+
+ int i, length = text.length();
+ if (getLength(text) >= length) {
+ return text;
+ }
+ StringBuilder sb = new StringBuilder();
+ int count = 1;
+ for (i = 1; i < length; i++) {
+ if (text.charAt(i) == text.charAt(i - 1)) {
+ ++count;
+ } else {
+ sb.append(text.charAt(i - 1)).append(count);
+ count = 1;
+ }
+ }
+ sb.append(text.charAt(i - 1)).append(count);
+ return sb.toString();
+ }
+
+ int getLength(String text) {
+ int size = 0;
+ int i, length = text.length();
+ int count = 1;
+ for (i = 1; i < length; i++) {
+ if (text.charAt(i) == text.charAt(i - 1)) {
+ ++count;
+ } else {
+ size += 1 + String.valueOf(count).length();
+ count = 1;
+ }
+ }
+ size += 1 + String.valueOf(count).length();
+ return size;
+ }
+
+ public static void main(String[] args) {
+ Q1_5_CompressText q1 = new Q1_5_CompressText();
+ String encrypt = q1.compress("aaaahhhuuuu");
+ System.out.println(encrypt);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_6_RotateMatrix.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_6_RotateMatrix.java
new file mode 100644
index 0000000..73dc789
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_6_RotateMatrix.java
@@ -0,0 +1,37 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ArraysAndStrings;
+
+public class Q1_6_RotateMatrix {
+
+ public void rotate(int[][] matrix) {
+ int edge = matrix.length, i, level;
+ for (level = 0; level < edge / 2; level++) {
+ for (i = 0; i < edge - 2 * level - 1; i++) {
+ int temp = matrix[level][i];
+ matrix[level][level + i] = matrix[ edge - level - 1 - i][level];
+ matrix[edge - level - 1 - i][level] = matrix[edge - 1 - level - i][level];
+ matrix[edge - 1 - level - i][level] = matrix[edge - level - 1][edge - level - 1 - i];
+ matrix[edge - level - 1][edge - level - 1 - i] = matrix[level + i][edge - level - 1];
+ matrix[level + i][edge - level - 1] = temp;
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ int[][] matrix = {
+ {1, 2, 3},
+ {4, 5, 6},
+ {7, 8, 9}
+ };
+ new Q1_6_RotateMatrix().rotate(matrix);
+ int edge = matrix.length;
+ for (int i = 0; i < edge; i++) {
+ for (int j = 0; j < edge; j++) {
+ System.out.print(matrix[i][j]);
+ }
+ System.out.println();
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_7_SetZero.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_7_SetZero.java
new file mode 100644
index 0000000..476272d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_7_SetZero.java
@@ -0,0 +1,30 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ArraysAndStrings;
+
+public class Q1_7_SetZero {
+
+ void setZero(int[][] matrix) {
+ int col, row, height = matrix.length, width = matrix[0].length;
+ boolean[] col_list = new boolean[height], row_list = new boolean[width];
+ for (col = 0; col < height; col++) {
+ for (row = 0; row < width; row++) {
+ if (matrix[col][row] == 0) {
+ col_list[col] = true;
+ row_list[row] = true;
+ }
+ }
+ }
+ for (col = 0; col < height; col++) {
+ for (row = 0; row < width; row++) {
+ if (col_list[col] || row_list[row]) {
+ matrix[col][row] = 0;
+ }
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_8_IsRotate.java b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_8_IsRotate.java
new file mode 100644
index 0000000..42a14fb
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ArraysAndStrings/Q1_8_IsRotate.java
@@ -0,0 +1,19 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.ArraysAndStrings;
+
+public class Q1_8_IsRotate {
+
+ boolean isRotate(String text1, String text2)
+ {
+ return (text1+text1).contains(text2);
+ }
+
+ public static void main(String[] args) {
+ Q1_8_IsRotate q1 = new Q1_8_IsRotate();
+ boolean result = q1.isRotate("abc", "cab");
+ System.out.println(result);
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_1_Add.java b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_1_Add.java
new file mode 100644
index 0000000..12ed13a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_1_Add.java
@@ -0,0 +1,23 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Hard;
+
+public class Q18_1_Add {
+
+ public int add(int a, int b) {
+ if (b == 0) {
+ return a;
+ }
+ int addition = a ^ b;
+ int carry = (a & b) << 1;
+ return add(addition, carry);
+ }
+
+ public static void main(String[] args) {
+ int a = 10;
+ int b = 200;
+ int add = new Q18_1_Add().add(a, b);
+ System.out.println(add);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_2_ShuffleCard.java b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_2_ShuffleCard.java
new file mode 100644
index 0000000..5b1f18a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_2_ShuffleCard.java
@@ -0,0 +1,20 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Hard;
+
+public class Q18_2_ShuffleCard {
+
+ public void shuffle(int[] cards) {
+ int i, length = cards.length;
+ for (i = 0; i < length - 1; i++) {
+ int r = (int) (Math.random() * (length - i)) + i;
+ int temp = cards[r];
+ cards[r] = cards[i];
+ cards[i] = temp;
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_3_RandomInt.java b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_3_RandomInt.java
new file mode 100644
index 0000000..136fc58
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_3_RandomInt.java
@@ -0,0 +1,21 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Hard;
+
+public class Q18_3_RandomInt {
+
+ public int[] generate(int[] original, int m) {
+ int[] subset = new int[m];
+ int[] array = original.clone();
+ for (int i = 0; i < m; i++) {
+ int j = i + (int) (Math.random() * (m - i));
+ subset[i] = array[j];
+ array[j] = array[i];
+ }
+ return subset;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_4_Count2InN.java b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_4_Count2InN.java
new file mode 100644
index 0000000..55509ea
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Hard/Q18_4_Count2InN.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Hard;
+public class Q18_4_Count2InN {
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_1_RemoveDuplicate.java b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_1_RemoveDuplicate.java
new file mode 100644
index 0000000..440dd58
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_1_RemoveDuplicate.java
@@ -0,0 +1,50 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.LinkedLists;
+
+import Util.LinkedListNode;
+import java.util.HashSet;
+
+public class Q2_1_RemoveDuplicate {
+
+ void removeDuplicate(LinkedListNode head)
+ {
+ LinkedListNode loop = head.Next, pre = head;
+ HashSet tab = new HashSet<>();
+ while (loop != null) {
+ if (tab.contains(loop.Data)) {
+ pre.Next = loop.Next;
+ }else{
+ tab.add(loop.Data);
+ pre = loop;
+ }
+ loop = loop.Next;
+ }
+ }
+
+ void removeDuplicate2(LinkedListNode head)
+ {
+ LinkedListNode loop = head.Next, pre = head, runner = head;
+ while (loop != null) {
+ runner = loop.Next;
+ pre = loop;
+ while (runner!= null) {
+ if (runner.Data == loop.Data) {
+ pre.Next = runner.Next;
+ }else{
+ pre = runner;
+ }
+ runner = runner.Next;
+ }
+ loop = loop.Next;
+ }
+ }
+
+ public static void main(String[] args) {
+ LinkedListNode head = LinkedListNode.getSampleLinkedList4();
+ //new Q2_1_RemoveDuplicate().removeDuplicate(head);
+ new Q2_1_RemoveDuplicate().removeDuplicate2(head);
+ head.print();
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_2_KthLastNode.java b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_2_KthLastNode.java
new file mode 100644
index 0000000..c972047
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_2_KthLastNode.java
@@ -0,0 +1,69 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.LinkedLists;
+
+import Util.LinkedListNode;
+
+public class Q2_2_KthLastNode {
+
+ LinkedListNode getKthLastNode3(LinkedListNode head, int k) {
+ LinkedListNode left = head, right = head;
+ while (k-- > 0) {
+ right = right.Next;
+ }
+ while (right != null) {
+ left = left.Next;
+ right = right.Next;
+ }
+ return left;
+ }
+ static int count = 1;
+
+ LinkedListNode getKthLastNode2(LinkedListNode head, int k) {
+ if (head == null) {
+ return null;
+ }
+ LinkedListNode node = getKthLastNode2(head.Next, k);
+ if (count <= k) {
+ ++count;
+ return head;
+ }
+ return node;
+ }
+
+ LinkedListNode getKthLastNode(LinkedListNode head, Wrapper wr, int k) {
+ if (head == null) {
+ return null;
+ }
+ if (wr == null) {
+ wr = new Wrapper(1);
+ }
+ LinkedListNode node = getKthLastNode(head.Next, wr, k);
+ if (wr.count <= k) {
+ ++wr.count;
+ return head;
+ }
+ return node;
+ }
+
+ class Wrapper {
+
+ int count;
+
+ public Wrapper(int _count) {
+ this.count = _count;
+ }
+ }
+
+ public static void main(String[] args) {
+ LinkedListNode head = LinkedListNode.getSampleLinkedList2();
+ Q2_2_KthLastNode q2 = new Q2_2_KthLastNode();
+ LinkedListNode node1 = q2.getKthLastNode(head, null, 2);
+ System.out.println(node1.Data);
+ LinkedListNode node2 = q2.getKthLastNode2(head, 2);
+ System.out.println(node2.Data);
+ LinkedListNode node3 = q2.getKthLastNode3(head, 2);
+ System.out.println(node3.Data);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_3_DeleteNode.java b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_3_DeleteNode.java
new file mode 100644
index 0000000..dc11725
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_3_DeleteNode.java
@@ -0,0 +1,24 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.LinkedLists;
+
+import Util.LinkedListNode;
+
+public class Q2_3_DeleteNode {
+
+ void delete(LinkedListNode node)
+ {
+ if (node.Next == null) {
+ System.out.println("No ways");
+ //throw new Exception("UnSupported");
+ throw new RuntimeException("UnSupported");
+ }else{
+ node.Data = node.Next.Data;
+ node.Next = node.Next.Next;
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_4_PartitionList.java b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_4_PartitionList.java
new file mode 100644
index 0000000..c3e3d6e
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_4_PartitionList.java
@@ -0,0 +1,90 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.LinkedLists;
+
+import Util.LinkedListNode;
+
+public class Q2_4_PartitionList {
+
+ LinkedListNode partition2(LinkedListNode head, int pivot) {
+ LinkedListNode Left = null, Right = null, cur = head, next = cur.Next;
+ while (cur != null) {
+ next = cur.Next;
+ int val = cur.Data;
+ if (val <= pivot) {
+ if (Left == null) {
+ Left = cur;
+ Left.Next = null;
+ } else {
+ cur.Next = Left;
+ Left = cur;
+ }
+ } else {
+ if (Right == null) {
+ Right = cur;
+ Right.Next = null;
+ } else {
+ cur.Next = Right;
+ Right = cur;
+ }
+ }
+ cur = next;
+ }
+ LinkedListNode Connecter = Left;
+ while (Connecter.Next != null) {
+ Connecter = Connecter.Next;
+ }
+ System.out.println("Pivot:" + Connecter.Data);
+ Connecter.Next = Right;
+ return Left;
+ }
+
+ LinkedListNode partition1(LinkedListNode head, int pivot) {
+ LinkedListNode LeftStart = null, LeftEnd = null, RightStart = null, RightEnd = null, cur = head, next = cur.Next;
+ while (cur != null) {
+ next = cur.Next;
+ int val = cur.Data;
+ if (val <= pivot) {
+ if (LeftStart == null || LeftEnd == null) {
+ LeftStart = cur;
+ LeftEnd = cur;
+ } else {
+ LeftEnd.Next = cur;
+ LeftEnd = cur;
+ }
+ } else {
+ if (RightStart == null || RightEnd == null) {
+ RightStart = cur;
+ RightEnd = cur;
+ } else {
+ cur.Next = RightStart;
+ RightStart = cur;
+ }
+ }
+ cur = next;
+ }
+ if (RightEnd != null) {
+ RightEnd.Next = null;
+ }
+ if (LeftEnd != null) {
+ LeftEnd.Next = RightStart;
+ }
+ if (LeftStart != null) {
+ return LeftStart;
+ } else {
+ return RightStart;
+ }
+ }
+
+ public static void main(String[] args) {
+ LinkedListNode head = LinkedListNode.getSampleLinkedList5();
+ head.print();
+
+ Q2_4_PartitionList q24 = new Q2_4_PartitionList();
+ //LinkedListNode head1 = q24.partition1(head, 4);
+ //head1.print();
+ LinkedListNode head2 = q24.partition2(head, 3);
+ head2.print();
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_5_Addition.java b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_5_Addition.java
new file mode 100644
index 0000000..c6e61e7
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_5_Addition.java
@@ -0,0 +1,90 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.LinkedLists;
+
+import Util.LinkedListNode;
+
+public class Q2_5_Addition {
+
+ public LinkedListNode add2(LinkedListNode left, LinkedListNode right, int carry) {
+ int sum = carry;
+ if (left != null) {
+ sum += left.Data;
+ }
+ if (right != null) {
+ sum += right.Data;
+ }
+ int digit = sum % 10;
+ int next_carry = sum / 10;
+ LinkedListNode node = new LinkedListNode<>(digit);
+ if (next_carry > 0 || left != null || right != null) {
+ LinkedListNode next = add2(left.Next, right.Next, next_carry);
+ node.Next = next;
+ }
+ return node;
+ }
+
+ public LinkedListNode add1(LinkedListNode left, LinkedListNode right) {
+ int l_length = left.getLength();
+ int r_length = right.getLength();
+ if (l_length >= r_length) {
+ int count = l_length - r_length;
+ right = padEmplty(right, count);
+ } else {
+ int count = r_length - l_length;
+ left = padEmplty(left, count);
+ }
+ Wrapper wr = addHelper(left, right);
+ if (wr.carry > 0) {
+ LinkedListNode node = new LinkedListNode<>(wr.carry);
+ node.Next = wr.node;
+ return node;
+ } else {
+ return wr.node;
+ }
+ }
+
+ private Wrapper addHelper(LinkedListNode left, LinkedListNode right) {
+ if (left == null || right == null) {
+ return new Wrapper(null, 0);
+ }
+ Wrapper wr = addHelper(left.Next, right.Next);
+ int sum = left.Data + right.Data + wr.carry;
+ int digit = sum % 10;
+ int carry = sum / 10;
+ LinkedListNode node = new LinkedListNode<>(digit);
+ node.Next = wr.node;
+ return new Wrapper(node, carry);
+ }
+
+ class Wrapper {
+
+ int carry;
+ LinkedListNode node = null;
+
+ public Wrapper(LinkedListNode _node, int _carry) {
+ this.node = _node;
+ this.carry = _carry;
+ }
+ }
+
+ private LinkedListNode padEmplty(LinkedListNode head, int count) {
+ while (count-- > 0) {
+ LinkedListNode node = new LinkedListNode<>(0);
+ node.Next = head;
+ head = node;
+ }
+ return head;
+ }
+
+ public static void main(String[] args) {
+ Q2_5_Addition q25 = new Q2_5_Addition();
+ LinkedListNode left = LinkedListNode.getSampleLinkedList2();
+ LinkedListNode right = LinkedListNode.getSampleLinkedList2();
+ LinkedListNode head1 = q25.add1(left, right);
+ head1.print();
+ LinkedListNode head2 = q25.add2(left, right, 0);
+ head2.print();
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_6_FindBeginning.java b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_6_FindBeginning.java
new file mode 100644
index 0000000..c88b1a4
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_6_FindBeginning.java
@@ -0,0 +1,33 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.LinkedLists;
+
+import Util.LinkedListNode;
+
+public class Q2_6_FindBeginning {
+
+ LinkedListNode findBegin(LinkedListNode head) {
+ LinkedListNode fast = head, slow = head;
+ do {
+ if (fast == null || slow == null || fast.Next == null) {
+ System.out.println("No Loop!!!");
+ return null;
+ }
+ fast = fast.Next.Next;
+ slow = slow.Next;
+ } while (fast != slow);
+ slow = head;
+ while (slow != fast) {
+ slow = slow.Next;
+ fast = fast.Next;
+ }
+ return fast;
+ }
+
+ public static void main(String[] args) {
+ LinkedListNode head = LinkedListNode.getSampleLinkedList6();
+ LinkedListNode begin = new Q2_6_FindBeginning().findBegin(head);
+ System.out.println(begin.Data);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_7_IsPalindrome.java b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_7_IsPalindrome.java
new file mode 100644
index 0000000..293d700
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/LinkedLists/Q2_7_IsPalindrome.java
@@ -0,0 +1,73 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 2, 2013
+ */
+package CareerCup.LinkedLists;
+
+import Util.LinkedListNode;
+import java.util.Stack;
+
+public class Q2_7_IsPalindrome {
+
+ boolean isPalindrome2(LinkedListNode head) {
+ Stack stack = new Stack<>();
+ LinkedListNode fast = head, slow = head;
+ while (fast != null && fast.Next != null) {
+ stack.push(slow.Data);
+ slow = slow.Next;
+ fast = fast.Next.Next;
+ }
+ if (fast != null) {
+ slow = slow.Next;
+ }
+ while (!stack.isEmpty()) {
+ if (stack.pop() != slow.Data) {
+ return false;
+ }
+ slow = slow.Next;
+ }
+ return true;
+ }
+
+ boolean isPalindrome(LinkedListNode head) {
+ int length = head.getLength();
+ Wrapper wr = isPalindrome(head, length);
+ return wr.match;
+ }
+
+ Wrapper isPalindrome(LinkedListNode head, int length) {
+ if (length == 2) {
+ return new Wrapper(head.Next.Next, head.Data == head.Next.Data);
+ } else if (length == 1) {
+ return new Wrapper(head.Next, true);
+ } else if (length == 0) {
+ return new Wrapper(head, true);
+ }
+ Wrapper wr = isPalindrome(head.Next, length - 2);
+ if (!wr.match || wr.node == null) {
+ return wr;
+ }
+ wr.match = head.Data == wr.node.Data;
+ wr.node = wr.node.Next;
+ return wr;
+ }
+
+ class Wrapper {
+
+ LinkedListNode node;
+ boolean match;
+
+ public Wrapper(LinkedListNode _node, boolean _match) {
+ this.match = _match;
+ this.node = _node;
+ }
+ }
+
+ public static void main(String[] args) {
+ LinkedListNode head = LinkedListNode.getSampleLinkedList7();
+ Q2_7_IsPalindrome q27 = new Q2_7_IsPalindrome();
+ boolean result = q27.isPalindrome(head);
+ System.out.println(result);
+ boolean result2 = q27.isPalindrome2(head);
+ System.out.println(result2);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_3_Line/Line.java b/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_3_Line/Line.java
new file mode 100644
index 0000000..964087c
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_3_Line/Line.java
@@ -0,0 +1,68 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.MathematicsAndProbability.Q7_3_Line;
+
+import java.util.HashMap;
+import sun.net.www.content.audio.x_aiff;
+
+public class Line {
+
+ final static double epsilon = 0.00001;
+ public double slope;
+ public double yintercept;
+ public boolean infinite_slope;
+
+ public Line(double _slope, double _yintercept) {
+ this.yintercept = _yintercept;
+ this.slope = _slope;
+ //y = slope * x + yintercept
+ }
+
+ public Line(Point p1, Point p2) {
+ if (Math.abs(p2.x - p1.x) > epsilon) {
+ this.slope = (p2.y - p1.y) / (p2.x - p1.x);
+ this.infinite_slope = false;
+ } else {
+ this.infinite_slope = true;
+ }
+ if (infinite_slope) {
+ this.yintercept = p1.x;
+ } else {
+ this.yintercept = p1.y - slope * p1.x;
+ }
+
+ }
+
+ @Override
+ public int hashCode() {
+ //return super.hashCode();
+ return (int) (100 * slope) * (int) (yintercept * 100) * (infinite_slope ? 1 : -1);
+ }
+
+ public boolean isIntersect(Line another) {
+ return Math.abs(this.slope - another.slope) > epsilon
+ || Math.abs(this.yintercept - another.yintercept) < epsilon;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ //return super.equals(obj);
+ Line another = (Line) obj;
+ return Math.abs(this.slope - another.slope) < epsilon
+ && Math.abs(this.yintercept - another.yintercept) < epsilon
+ && this.infinite_slope == another.infinite_slope;
+ }
+
+ public static void main(String[] args) {
+ Line l1 = new Line(new Point(0, 0), new Point(1, 1));
+ Line l2 = new Line(new Point(0, 0), new Point(-1, -1));
+ Line l3 = new Line(new Point(0, 0), new Point(2, 2));
+ Line l4 = new Line(new Point(2, 2), new Point(1, 1));
+ HashMap count = new HashMap<>();
+ count.put(l1, 4);
+ count.put(l1, 3);
+ count.put(l1, 2);
+ count.put(l1, 1);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_3_Line/Point.java b/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_3_Line/Point.java
new file mode 100644
index 0000000..3dd8882
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_3_Line/Point.java
@@ -0,0 +1,18 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.MathematicsAndProbability.Q7_3_Line;
+
+public class Point {
+
+ public double x;
+ public double y;
+
+ public Point(double _x, double _y) {
+ this.x = _x;
+ this.y = _y;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_7_UnglyNumber/UnglyNumber.java b/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_7_UnglyNumber/UnglyNumber.java
new file mode 100644
index 0000000..472a41c
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/MathematicsAndProbability/Q7_7_UnglyNumber/UnglyNumber.java
@@ -0,0 +1,43 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.MathematicsAndProbability.Q7_7_UnglyNumber;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class UnglyNumber {
+
+ public int getKthUnglyNumber(int k) {
+ Queue s3 = new LinkedList<>();
+ Queue s5 = new LinkedList<>();
+ Queue s7 = new LinkedList<>();
+ int val = 1;
+ s3.add(1);
+ while (--k > 0) {
+ int v3 = s3.isEmpty() ? Integer.MAX_VALUE : s3.peek();
+ int v5 = s5.isEmpty() ? Integer.MAX_VALUE : s5.peek();
+ int v7 = s7.isEmpty() ? Integer.MAX_VALUE : s7.peek();
+ val = Math.min(v7, Math.min(v3, v5));
+ if (val == v3) {
+ s3.poll();
+ s3.add(val * 3);
+ s5.add(val * 5);
+ s7.add(val * 7);
+ } else if (val == v5) {
+ s5.poll();
+ s5.add(val * 5);
+ s7.add(val * 7);
+ } else if (val == v7) {
+ s7.poll();
+ s7.add(val * 7);
+ }
+ System.out.println(val);
+ }
+ return val;
+ }
+
+ public static void main(String[] args) {
+ new UnglyNumber().getKthUnglyNumber(100);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_10_EncodeXML.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_10_EncodeXML.java
new file mode 100644
index 0000000..9b1c107
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_10_EncodeXML.java
@@ -0,0 +1,14 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_10_EncodeXML {
+
+ public void encode(StringBuffer sb) {
+
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_11_Random7.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_11_Random7.java
new file mode 100644
index 0000000..f7815c0
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_11_Random7.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Moderate;
+public class Q17_11_Random7 {
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_12_PairToSum.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_12_PairToSum.java
new file mode 100644
index 0000000..5df3eab
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_12_PairToSum.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Moderate;
+public class Q17_12_PairToSum {
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_13_BinaryTree2LinkedList.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_13_BinaryTree2LinkedList.java
new file mode 100644
index 0000000..55d76f1
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_13_BinaryTree2LinkedList.java
@@ -0,0 +1,10 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_13_BinaryTree2LinkedList {
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_14_ParseWord.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_14_ParseWord.java
new file mode 100644
index 0000000..bdec870
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_14_ParseWord.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Moderate;
+public class Q17_14_ParseWord {
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_1_Swap.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_1_Swap.java
new file mode 100644
index 0000000..6329038
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_1_Swap.java
@@ -0,0 +1,30 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_1_Swap {
+
+ void swap(int a, int b) {
+ //diff = a - b
+ a = a - b;
+ b = a + b;
+ a = b - a;
+ System.out.println("a:" + a);
+ System.out.println("b:" + b);
+ }
+
+ void swap2(int a, int b) {
+ a = a ^ b;
+ b = a ^ b;
+ a = a ^ b;
+ System.out.println("a:" + a);
+ System.out.println("b:" + b);
+ }
+
+ public static void main(String[] args) {
+ Q17_1_Swap swap = new Q17_1_Swap();
+ swap.swap(1, 3);
+ swap.swap2(3, 4);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_2_TicTacToe.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_2_TicTacToe.java
new file mode 100644
index 0000000..195a070
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_2_TicTacToe.java
@@ -0,0 +1,71 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_2_TicTacToe {
+
+ enum Piece {
+
+ X, O, Empty
+ }
+
+ public Piece hasWon(Piece[][] board) {
+ int n = board.length;
+ int col, row;
+ //Check row
+ for (col = 0; col < n; col++) {
+ if (board[col][0] == Piece.Empty) {
+ break;
+ }
+ for (row = 1; row < n; row++) {
+ if (board[col][row] == Piece.Empty || board[col][row] != board[col][row - 1]) {
+ break;
+ }
+ if (row == n - 1) {
+ return board[col][row];
+ }
+ }
+ }
+ //check col
+ for (row = 0; row < n; row++) {
+ if (board[col][row] == Piece.Empty) {
+ break;
+ }
+ for (col = 1; col < n; col++) {
+ if (board[col][row] == Piece.Empty || board[col][row] != board[col - 1][row]) {
+ break;
+ }
+ }
+ if (col == n - 1) {
+ return board[col][row];
+ }
+ }
+ //diagonal
+ if (board[0][0] != Piece.Empty) {
+ for (col = 1; col < n; col++) {
+ if (board[col][col] == Piece.Empty || board[col][col] != board[col - 1][col - 1]) {
+ break;
+ }
+ if (col == n - 1) {
+ return board[col][col];
+ }
+ }
+ }
+ //reverse diagonal
+ if (board[0][n - 1] != Piece.Empty) {
+ for (col = 1; col < n; col++) {
+ if (board[col][n - 1 - col] == Piece.Empty || board[col][n - 1 - col] != board[col - 1][n - col]) {
+ break;
+ }
+ if (col == n - 1) {
+ return board[col][n - 1 - col];
+ }
+ }
+ }
+ return Piece.Empty;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_3_ZeroInFactorial.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_3_ZeroInFactorial.java
new file mode 100644
index 0000000..d0e12f6
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_3_ZeroInFactorial.java
@@ -0,0 +1,41 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_3_ZeroInFactorial {
+
+ public int factorOf5(int n) {
+ int count = 0;
+ while (n % 5 == 0) {
+ ++count;
+ n /= 5;
+ }
+ return count;
+ }
+
+ public int countZero(int num) {
+ int count = 0;
+ for (int i = 2; i <= num; i++) {
+ count += factorOf5(i);
+ }
+ return count;
+ }
+
+ public int countZero2(int num) {
+ int count = 0;
+ for (int i = 5; num / i > 0; i *= 5) {
+ count += num / i;
+ }
+ return count;
+ }
+
+ public static void main(String[] args) {
+ int num = 19;
+ Q17_3_ZeroInFactorial q17 = new Q17_3_ZeroInFactorial();
+ int ways = q17.countZero(num);
+ System.out.println(ways);
+ int ways2 = q17.countZero2(num);
+ System.out.println(ways2);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_4_Max.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_4_Max.java
new file mode 100644
index 0000000..0a86326
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_4_Max.java
@@ -0,0 +1,38 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 29, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_4_Max {
+
+ public int max(int a, int b) {
+ int k = positive(a - b);
+ int q = flip(k);
+ return k * b + q * a;
+ }
+
+ public int flip(int n) {
+ return n ^ 0x1;
+ }
+
+ public int positive(int n) {
+ return (n >> 31) & 0x1;
+ }
+
+ public int max2(int a, int b) {
+ int sa = positive(a);
+ int sb = positive(b);
+ int sc = positive(a - b);
+ int use_sign_a = sa ^ sb;
+ int use_sign_c = flip(use_sign_a);
+ int k = use_sign_a * sa + use_sign_c * sc;
+ int q = flip(k);
+ return k * b + q * a;
+ }
+
+ public static void main(String[] args) {
+ Q17_4_Max q174 = new Q17_4_Max();
+ System.out.println(q174.max(-3, 9));
+ System.out.println(q174.max2(3, 5));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_5_MasterMind.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_5_MasterMind.java
new file mode 100644
index 0000000..c2c9c0f
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_5_MasterMind.java
@@ -0,0 +1,62 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 29, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_5_MasterMind {
+
+ public class Result {
+
+ public int hits = 0;
+ public int pseudo_hits = 0;
+
+ @Override
+ public String toString() {
+ return "Hits:" + hits + " PseudoHits:" + pseudo_hits;
+ }
+ }
+
+ public int code(char c) {
+ switch (c) {
+ case 'B':
+ return 0;
+ case 'G':
+ return 1;
+ case 'R':
+ return 2;
+ case 'Y':
+ return 3;
+ default:
+ return -1;
+ }
+ }
+ final short NUM_COLORS = 4;
+
+ public Result estimate(String guess, String solution) {
+ if (guess.length() != solution.length()) {
+ return null;
+ }
+ Result r = new Result();
+ short frequency[] = new short[NUM_COLORS];
+ int i, length = solution.length();
+ for (i = 0; i < length; i++) {
+ if (guess.charAt(i) == solution.charAt(i)) {
+ ++r.hits;
+ } else {
+ ++frequency[code(solution.charAt(i))];
+ }
+ }
+ for (i = 0; i < length; i++) {
+ if (guess.charAt(i) != solution.charAt(i)
+ && frequency[code(guess.charAt(i))] > 0
+ && code(guess.charAt(i)) >= 0) {
+ --frequency[code(guess.charAt(i))];
+ ++r.pseudo_hits;
+ }
+ }
+ return r;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_6_FindMNToBeSorted.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_6_FindMNToBeSorted.java
new file mode 100644
index 0000000..e98f9bf
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_6_FindMNToBeSorted.java
@@ -0,0 +1,75 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 29, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_6_FindMNToBeSorted {
+
+ public int findEndOfLeftSubsequence(int[] arr) {
+ int i, length = arr.length;
+ for (i = 1; i < length; i++) {
+ if (arr[i] < arr[i - 1]) {
+ break;
+ }
+ }
+ return i - 1;
+ }
+
+ public int findStartOfRightSubsequence(int[] arr) {
+ int i, length = arr.length;
+ for (i = length - 2; i >= 0; i--) {
+ if (arr[i] > arr[i + 1]) {
+ break;
+ }
+ }
+ return i + 1;
+ }
+
+ public int shrinkLeft(int[] arr, int min_index, int start) {
+ int comp = arr[min_index], i;
+ for (i = start - 1; i >= 0; --i) {
+ if (arr[i] <= comp) {
+ return i + 1;
+ }
+ }
+ return 0;
+ }
+
+ public int shrinkRight(int[] arr, int max_index, int start) {
+ int comp = arr[max_index], i, length = arr.length;
+ for (i = start + 1; i < length - 1; i++) {
+ if (arr[i] >= comp) {
+ return i - 1;
+ }
+ }
+ return length - 1;
+ }
+
+ public void findMN(int[] arr) {
+ int length = arr.length, i;
+ int end_left = findEndOfLeftSubsequence(arr);
+ int start_right = findStartOfRightSubsequence(arr);
+
+ int max_index = start_right - 1;
+ int min_index = end_left + 1;
+ if (min_index == length || max_index < 0) {
+ return;
+ }
+ for (i = end_left; i <= start_right; i++) {
+ if (arr[i] > arr[max_index]) {
+ max_index = i;
+ }
+ if (arr[i] < arr[min_index]) {
+ min_index = i;
+ }
+ }
+ int m = shrinkLeft(arr, min_index, end_left);
+ int n = shrinkRight(arr, max_index, start_right);
+ System.out.println("m:" + m + " n:" + n);
+ }
+
+ public static void main(String[] args) {
+ int[] arr = {1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19};
+ new Q17_6_FindMNToBeSorted().findMN(arr);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_7_ConvertToEnglish.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_7_ConvertToEnglish.java
new file mode 100644
index 0000000..25d7030
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_7_ConvertToEnglish.java
@@ -0,0 +1,57 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 29, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_7_ConvertToEnglish {
+
+ final String[] bigs = {"Thousand", "Million", "Billion"};
+ final String[] digits = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
+ final String[] teens = {"Eleven", "Twelve", "Thirsteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
+ final String[] tens = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty"};
+
+ public String convert(int num) {
+ StringBuilder sb = new StringBuilder();
+ boolean negative = false;
+ if (num < 0) {
+ negative = true;
+ num = -num;
+ } else if (num == 0) {
+ return "Zero";
+ }
+ int count = -1;
+ while (num > 0) {
+ if (count >= 0) {
+ sb.insert(0, bigs[count]).insert(0, " ");;
+ }
+ sb.insert(0, convert100(num % 1000)).insert(0, " ");
+ num /= 1000;
+ ++count;
+ }
+ if (negative) {
+ sb.insert(0, "Negative ");
+ }
+ return sb.toString();
+ }
+
+ private String convert100(int num) {
+ StringBuilder sb = new StringBuilder();
+ if (num >= 100) {
+ sb.append(digits[ num / 100 - 1]).append(" Hundred ");
+ num %= 100;
+ }
+ if (num > 19) {
+ sb.append(tens[num / 10 - 1]).append(" ").append(digits[num % 10 - 1]).append(" ");
+ } else if (num >= 11) {
+ sb.append(teens[num % 10 - 1]).append(" ");
+ } else {
+ sb.append(digits[num % 10 - 1]).append(" ");
+ }
+ return sb.toString();
+ }
+
+ public static void main(String[] args) {
+ Q17_7_ConvertToEnglish q17 = new Q17_7_ConvertToEnglish();
+ System.out.println(q17.convert(3111234));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_8_LargestSum.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_8_LargestSum.java
new file mode 100644
index 0000000..384a2e9
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_8_LargestSum.java
@@ -0,0 +1,33 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 29, 2012
+ */
+package CareerCup.Moderate;
+
+public class Q17_8_LargestSum {
+
+ public int sum(int[] arr) {
+ int max = arr[0], sum = arr[0], i, length = arr.length;
+ for (i = 1; i < length; i++) {
+ sum += arr[i];
+ if (sum > max) {
+ max = sum;
+ } else if (sum < 0) {
+ sum = 0;
+ }
+ }
+ if (max <= 0) {
+ for (i = 0; i < length; i++) {
+ if (arr[i] > max) {
+ max = arr[i];
+ }
+ }
+ }
+ return max;
+ }
+
+ public static void main(String[] args) {
+ int[] arr = {1, 2, 3, -2, -1, 9, -100};
+ int sum = new Q17_8_LargestSum().sum(arr);
+ System.out.println(sum);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_9_FrequencyWordInBook.java b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_9_FrequencyWordInBook.java
new file mode 100644
index 0000000..0d59633
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/Moderate/Q17_9_FrequencyWordInBook.java
@@ -0,0 +1,7 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.Moderate;
+public class Q17_9_FrequencyWordInBook {
+
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_10_HashTable/Entry.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_10_HashTable/Entry.java
new file mode 100644
index 0000000..47d73bf
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_10_HashTable/Entry.java
@@ -0,0 +1,72 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 18, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_10_HashTable;
+
+public class Entry {
+
+ private V Value;
+
+ public V getValue() {
+ return Value;
+ }
+
+ public void setValue(V Value) {
+ this.Value = Value;
+ }
+ private K Key;
+
+ public K getKey() {
+ return Key;
+ }
+
+ public void setKey(K Key) {
+ this.Key = Key;
+ }
+ private Entry Next;
+
+ public Entry getNext() {
+ return Next;
+ }
+
+ public void setNext(Entry Next) {
+ this.Next = Next;
+ }
+
+ public Entry(K _k, V _v) {
+ this.Key = _k;
+ this.Value = _v;
+ this.Next = null;
+ }
+
+ public void addEntry(Entry _e) {
+ Entry runner = this;
+ if (runner != null && runner.getKey() == _e.Key) {
+ runner.setValue(_e.Value);
+ return;
+ }
+ while (runner.Next != null) {
+ runner = runner.Next;
+ if (runner != null && runner.getKey() == _e.Key) {
+ runner.setValue(_e.Value);
+ return;
+ }
+ }
+ runner.Next = _e;
+ }
+
+ public void deleteEntry(K key) {
+ Entry runner = this;
+ while (runner != null) {
+ Entry next_entry = runner.getNext();
+ if (next_entry != null && next_entry.getKey() == key) {
+ runner.setNext(next_entry.getNext());
+ return;
+ }
+ runner = next_entry;
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_10_HashTable/HashTable.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_10_HashTable/HashTable.java
new file mode 100644
index 0000000..1907e9f
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_10_HashTable/HashTable.java
@@ -0,0 +1,66 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 18, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_10_HashTable;
+
+public class HashTable {
+
+ public int Capacity = 1000;
+ public Entry[] array;
+
+ public HashTable() {
+ array = new Entry[Capacity];
+ }
+
+ public HashTable(int _capacity) {
+ this.Capacity = _capacity;
+ array = new Entry[Capacity];
+ }
+
+ public synchronized void put(K key, V value) {
+ int hash = key.hashCode() % Capacity;
+ Entry new_entry = new Entry(key, value);
+ if (array[hash] == null) {
+ array[hash] = new_entry;
+ } else {
+ array[hash].addEntry(new_entry);
+ }
+ }
+
+ public synchronized V get(K key) {
+ int hash = key.hashCode() % Capacity;
+ Entry e = array[hash];
+ while (e != null && !e.getKey().equals(key)) {
+ e = e.getNext();
+ }
+ if (e == null) {
+ return null;
+ } else {
+ return e.getValue();
+ }
+ }
+
+ public synchronized void delete(K key) {
+ int hash = key.hashCode() % Capacity;
+ if (array[hash] != null && array[hash].getKey() == key) {
+ array[hash] = array[hash].getNext();
+ } else {
+ array[hash].deleteEntry(key);
+ }
+ }
+
+ public static void main(String[] args) {
+ HashTable ht = new HashTable<>(10);
+ ht.put(1, "xxx");
+ ht.put(2, "yyy");
+ ht.put(3, "zzz");
+ ht.put(4, "qqq");
+ System.out.println(ht.get(1));
+ System.out.println(ht.get(2));
+ //ht.delete(1);
+ System.out.println(ht.get(1));
+ //ht.delete(2);
+ ht.put(2, "ZSQ");
+ System.out.println(ht.get(2));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Card.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Card.java
new file mode 100644
index 0000000..f41776a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Card.java
@@ -0,0 +1,26 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_1_DeckCard;
+
+public abstract class Card {
+
+ int face_value;
+ Suit suit;
+ boolean avaiable;
+
+ public Card(int _val, Suit _suit) {
+ this.face_value = _val;
+ this.suit = _suit;
+ avaiable = true;
+ }
+
+ public void markAvailable(boolean isAvailable) {
+ this.avaiable = isAvailable;
+ }
+
+ public abstract int value();
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Deck.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Deck.java
new file mode 100644
index 0000000..da78730
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Deck.java
@@ -0,0 +1,60 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_1_DeckCard;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+public class Deck {
+
+ public ArrayList cards;
+ private int dealt_num = 0;
+
+ public Deck() {
+ this.cards = new ArrayList<>();
+ }
+
+ public int remainingCard() {
+ return cards.size() - dealt_num;
+ }
+
+ public T[] dealtHand(int num) {
+ if (num > remainingCard()) {
+ return null;
+ }
+ T[] card_hand = (T[]) new Card[num];
+ for (int i = 0; i < num; i++) {
+ T card = dealtCard();
+ if (card != null) {
+ card_hand[i] = card;
+ }
+ }
+ return card_hand;
+ }
+
+ public T dealtCard() {
+ if (remainingCard() == 0) {
+ return null;
+ }
+ T card = cards.get(dealt_num);
+ card.markAvailable(false);
+ ++dealt_num;
+ return card;
+ }
+
+ public void shuffle() {
+ Random r = new Random(System.currentTimeMillis());
+ int i, size = cards.size(), j;
+ for (i = 0; i < size; i++) {
+ j = r.nextInt(size);
+ T card1 = cards.get(i);
+ T card2 = cards.get(j);
+ cards.set(i, card2);
+ cards.set(j, card1);
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Hand.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Hand.java
new file mode 100644
index 0000000..223706e
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Hand.java
@@ -0,0 +1,30 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_1_DeckCard;
+
+import java.util.ArrayList;
+
+public class Hand {
+
+ public ArrayList cards_onhand;
+
+ public Hand() {
+ cards_onhand = new ArrayList<>();
+ }
+
+ public int value() {
+ int val = 0;
+ for (T t : cards_onhand) {
+ val += t.value();
+ }
+ return val;
+ }
+
+ public void add(T card) {
+ cards_onhand.add(card);
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Suit.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Suit.java
new file mode 100644
index 0000000..ae5202c
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_1_DeckCard/Suit.java
@@ -0,0 +1,36 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_1_DeckCard;
+
+public enum Suit {
+
+ Club(0),
+ Diamond(1),
+ Heart(2),
+ Spade(3);
+ int i;
+
+ Suit(int _i) {
+ this.i = _i;
+ }
+
+ public int getValue() {
+ return i;
+ }
+
+ public static Suit getSuitFromValue(int value) {
+ switch (value) {
+ case 0:
+ return Suit.Club;
+ case 1:
+ return Suit.Diamond;
+ case 2:
+ return Suit.Heart;
+ case 3:
+ return Suit.Spade;
+ default:
+ return null;
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Call.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Call.java
new file mode 100644
index 0000000..d4043ce
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Call.java
@@ -0,0 +1,27 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+public class Call {
+
+ public Rank rank;
+ public Caller caller;
+ public Employee handler;
+
+ public Rank incrementRank() {
+ if (rank == Rank.Responder) {
+ rank = Rank.Manager;
+ } else if (rank == Rank.Manager) {
+ rank = Rank.Director;
+ }
+ return null;
+ }
+
+ public void disconnect() {
+ System.out.println("Good Bye");
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/CallHandler.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/CallHandler.java
new file mode 100644
index 0000000..b0e74e4
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/CallHandler.java
@@ -0,0 +1,85 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class CallHandler {
+
+ final int NUM_LEVEL = 3;
+ final int NUM_RESPONDENT = 10;
+ final int NUM_MANAGER = 5;
+ final int NUM_DIRECTOR = 1;
+ private static CallHandler inst;
+ ArrayList employees[];
+ Queue callqueue[];
+
+ private CallHandler() {
+ int i;
+ employees = new ArrayList[NUM_LEVEL];
+ for (i = 0; i < NUM_LEVEL; i++) {
+ employees[i] = new ArrayList<>();
+ }
+ callqueue = new LinkedList[NUM_LEVEL];
+ for (i = 0; i < NUM_LEVEL; i++) {
+ callqueue[i] = new LinkedList<>();
+ }
+ for (i = 0; i < NUM_RESPONDENT; i++) {
+ employees[0].add(new Respondent());
+ }
+ for (i = 0; i < NUM_MANAGER; i++) {
+ employees[1].add(new Manager());
+ }
+ for (i = 0; i < NUM_DIRECTOR; i++) {
+ employees[2].add(new Director());
+ }
+ }
+
+ public static CallHandler getInstance() {
+ if (inst == null) {
+ inst = new CallHandler();
+ }
+ return inst;
+ }
+
+ public Employee getHandlerForCall(Call c) {
+ for (Rank i = c.rank; i != null; c.incrementRank()) {
+ ArrayList list = employees[i.getValue()];
+ for (Employee employee : list) {
+ if (employee.isFree()) {
+ //employee.receiveCall(c);
+ return employee;
+ }
+ }
+ }
+ return null;
+ }
+
+ public void dispatchCall(Call c) {
+ Employee employee = getHandlerForCall(c);
+ if (employee != null) {
+ employee.receiveCall(c);
+ c.handler = employee;
+ } else {
+ System.out.println("No Available employee");
+ callqueue[c.rank.getValue()].add(c);
+ }
+ }
+
+ public boolean assignCall(Employee e) {
+ for (int r = e.rank.getValue(); r >= 0; --r) {
+ Queue queue = callqueue[r];
+ if (queue.peek() != null) {
+ e.receiveCall(queue.poll());
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Caller.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Caller.java
new file mode 100644
index 0000000..0a70d40
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Caller.java
@@ -0,0 +1,16 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+public class Caller {
+
+ public String name;
+
+ public Caller(String _name) {
+ this.name = _name;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Director.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Director.java
new file mode 100644
index 0000000..86b678e
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Director.java
@@ -0,0 +1,14 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+public class Director extends Employee {
+
+ public Director() {
+ this.rank = Rank.Director;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Employee.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Employee.java
new file mode 100644
index 0000000..959e102
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Employee.java
@@ -0,0 +1,44 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+public abstract class Employee {
+
+ public Call current_call;
+ Rank rank;
+
+ public void receiveCall(Call c) {
+ current_call = c;
+ }
+
+ public boolean isFree() {
+ return current_call == null;
+ }
+
+ public void callComplete() {
+ if (current_call != null) {
+ current_call.disconnect();
+ current_call = null;
+ }
+ assignNewCall();
+ }
+
+ public void assignNewCall() {
+ if (isFree()) {
+ CallHandler.getInstance().assignCall(this);
+ }
+ }
+
+ public void escalateCall() {
+ if (current_call != null) {
+ current_call.incrementRank();
+ CallHandler.getInstance().dispatchCall(current_call);
+ current_call = null;
+ }
+ assignNewCall();
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Manager.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Manager.java
new file mode 100644
index 0000000..87cf539
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Manager.java
@@ -0,0 +1,14 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+public class Manager extends Employee {
+
+ public Manager() {
+ this.rank = Rank.Manager;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Rank.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Rank.java
new file mode 100644
index 0000000..58e3122
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Rank.java
@@ -0,0 +1,20 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+public enum Rank {
+
+ Responder(0),
+ Manager(1),
+ Director(2);
+ private int val;
+
+ private Rank(int _val) {
+ this.val = _val;
+ }
+
+ public int getValue() {
+ return val;
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Respondent.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Respondent.java
new file mode 100644
index 0000000..3cbc4e2
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_2_CallCenter/Respondent.java
@@ -0,0 +1,14 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_2_CallCenter;
+
+public class Respondent extends Employee {
+
+ public Respondent() {
+ rank = Rank.Responder;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/CD.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/CD.java
new file mode 100644
index 0000000..ec997d8
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/CD.java
@@ -0,0 +1,12 @@
+/**
+ * Sanqiang Zhao Www.131X.Com
+ * Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_3_JukeBox;
+
+public class CD {
+
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/CDPlayer.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/CDPlayer.java
new file mode 100644
index 0000000..d7ef4e8
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/CDPlayer.java
@@ -0,0 +1,17 @@
+/**
+ * Sanqiang Zhao Www.131X.Com
+ * Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_3_JukeBox;
+
+public class CDPlayer {
+
+ CD cd;
+ PlayList playlist;
+
+
+
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/JukeBox.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/JukeBox.java
new file mode 100644
index 0000000..541c4c9
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/JukeBox.java
@@ -0,0 +1,19 @@
+/**
+ * Sanqiang Zhao Www.131X.Com
+ * Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_3_JukeBox;
+
+import java.util.Set;
+
+public class JukeBox {
+
+ User user;
+ CDPlayer player;
+ Set cds;
+ SongSelector cs;
+
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/PlayList.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/PlayList.java
new file mode 100644
index 0000000..3c65bed
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/PlayList.java
@@ -0,0 +1,24 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_3_JukeBox;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class PlayList {
+
+ private Queue playlist;
+ Song cur;
+
+ public PlayList(Queue _playlist) {
+ this.playlist = new LinkedList<>();
+ }
+
+ public void playNext() {
+ cur = playlist.poll();
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/Song.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/Song.java
new file mode 100644
index 0000000..f2b7384
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/Song.java
@@ -0,0 +1,14 @@
+/**
+ * Sanqiang Zhao Www.131X.Com
+ * Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_3_JukeBox;
+
+public class Song {
+
+ public String SongName;
+
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/SongSelector.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/SongSelector.java
new file mode 100644
index 0000000..318031d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/SongSelector.java
@@ -0,0 +1,14 @@
+/**
+ * Sanqiang Zhao Www.131X.Com
+ * Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_3_JukeBox;
+
+public class SongSelector {
+
+ public Song cur_song;
+
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/User.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/User.java
new file mode 100644
index 0000000..a958224
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_3_JukeBox/User.java
@@ -0,0 +1,19 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_3_JukeBox;
+
+import java.math.BigInteger;
+
+public class User {
+
+ BigInteger id;
+ String name;
+
+ public User(String _name) {
+ this.name = _name;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Bus.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Bus.java
new file mode 100644
index 0000000..4990e03
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Bus.java
@@ -0,0 +1,18 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_4_ParkingLot;
+
+public class Bus extends Vehicle {
+
+ public Bus() {
+ super();
+ this.size = VehicleSize.Large;
+ this.SpotOccupy = 4;
+ }
+
+ @Override
+ public boolean canfit(ParkingSpot parkingSpot) {
+ return parkingSpot.SpotSize == VehicleSize.Large;
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Car.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Car.java
new file mode 100644
index 0000000..81bbe99
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Car.java
@@ -0,0 +1,21 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_4_ParkingLot;
+
+public class Car extends Vehicle {
+
+ public Car() {
+ super();
+ this.size = VehicleSize.Compact;
+ this.SpotOccupy = 2;
+ }
+
+ @Override
+ public boolean canfit(ParkingSpot parkingSpot) {
+ return parkingSpot.SpotSize == VehicleSize.Compact || parkingSpot.SpotSize == VehicleSize.Large;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Level.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Level.java
new file mode 100644
index 0000000..f896275
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Level.java
@@ -0,0 +1,79 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_4_ParkingLot;
+
+public class Level {
+
+ int numSpots;
+ int floor;
+ ParkingSpot[] parkingspots;
+ private static final int NUM_PER_ROW = 10;
+ private int availableSpot;
+
+ public Level(int _num_sports, int _floor) {
+ this.floor = _floor;
+ this.numSpots = _num_sports;
+ parkingspots = new ParkingSpot[_num_sports];
+ int large = _num_sports / 4;
+ int bike = _num_sports / 4;
+ int compact = _num_sports - large - bike;
+ for (int i = 0; i < _num_sports; i++) {
+ VehicleSize size = VehicleSize.Compact;
+ if (i < bike) {
+ size = VehicleSize.Motorcycle;
+ } else if (i < bike + large) {
+ size = VehicleSize.Large;
+ }
+ int row = i / NUM_PER_ROW;
+ parkingspots[i] = new ParkingSpot(size, i, row, _floor);
+ }
+ availableSpot = _num_sports;
+ }
+
+ public boolean partVehicle(Vehicle v) {
+ if (availableSpot < v.SpotOccupy) {
+ return false;
+ }
+ int FindSpot = findAvailableSpot(v);
+ if (FindSpot < 0) {
+ return false;
+ }
+ return partStartingAtSpot(v, FindSpot);
+ }
+
+ private boolean partStartingAtSpot(Vehicle v, int i) {
+ v.clearSpot();
+ boolean success = true;
+ for (int j = i; j < v.SpotOccupy; j++) {
+ success &= parkingspots[i].partVehicle(v);
+ }
+ if (success) {
+ availableSpot -= v.SpotOccupy;
+ }
+ return success;
+ }
+
+ private int findAvailableSpot(Vehicle v) {
+ int SpotNeed = v.SpotOccupy;
+ int LastRow = -1;
+ int SpotFind = 0;
+ for (int i = 0; i < numSpots; i++) {
+ ParkingSpot ps = parkingspots[i];
+ if (LastRow != ps.getRow()) {
+ LastRow = ps.getRow();
+ SpotFind = 0;
+ }
+ if (ps.canFitVehicle(v)) {
+ ++SpotFind;
+ }
+ if (SpotFind == SpotNeed) {
+ return i - (SpotNeed - 1);
+ }
+ }
+ return -1;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/ParkingLot.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/ParkingLot.java
new file mode 100644
index 0000000..545da56
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/ParkingLot.java
@@ -0,0 +1,26 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_4_ParkingLot;
+
+public class ParkingLot {
+
+ private Level[] levels;
+ private final int num_floor = 5;
+
+ public ParkingLot() {
+ levels = new Level[num_floor];
+ }
+
+ public boolean partAtLot(Vehicle v) {
+ for (int i = 0; i < num_floor; i++) {
+ if (levels[i].partVehicle(v)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/ParkingSpot.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/ParkingSpot.java
new file mode 100644
index 0000000..83f8c58
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/ParkingSpot.java
@@ -0,0 +1,47 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_4_ParkingLot;
+
+public class ParkingSpot {
+
+ private Vehicle CurrentVehicle;
+ private int num, row;
+ public VehicleSize SpotSize;
+ int floor;
+
+ public ParkingSpot(VehicleSize _size, int _num, int _row, int _floor) {
+ this.SpotSize = _size;
+ this.num = _num;
+ this.row = _row;
+ this.floor = _floor;
+ }
+
+ public int getRow(){
+ return row;
+ }
+
+ public void removeVehicle() {
+ CurrentVehicle = null;
+ }
+
+ private boolean isAvailable() {
+ return CurrentVehicle == null;
+ }
+
+ public boolean canFitVehicle(Vehicle v) {
+ return isAvailable() && v.canfit(this);
+ }
+
+ public boolean partVehicle(Vehicle v) {
+ if (!canFitVehicle(v)) {
+ return false;
+ }
+ CurrentVehicle = v;
+ v.partInSpot(this);
+ return true;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Vehicle.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Vehicle.java
new file mode 100644
index 0000000..1cf4ac3
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/Vehicle.java
@@ -0,0 +1,33 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_4_ParkingLot;
+
+import java.util.ArrayList;
+
+public abstract class Vehicle {
+
+ public VehicleSize size;
+ public int SpotOccupy;
+ public ArrayList ParkingSpots;
+
+ public Vehicle() {
+ this.ParkingSpots = new ArrayList<>();
+ }
+
+ public void partInSpot(ParkingSpot ps) {
+ this.ParkingSpots.add(ps);
+ }
+
+ public void clearSpot() {
+ for (ParkingSpot parkingSpot : ParkingSpots) {
+ parkingSpot.removeVehicle();
+ }
+ this.ParkingSpots.clear();
+ }
+
+ public abstract boolean canfit(ParkingSpot parkingSpot);
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/VehicleSize.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/VehicleSize.java
new file mode 100644
index 0000000..167ffa6
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_4_ParkingLot/VehicleSize.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_4_ParkingLot;
+
+public enum VehicleSize {
+
+ Motorcycle, Compact, Large
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Book.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Book.java
new file mode 100644
index 0000000..fdbea2d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Book.java
@@ -0,0 +1,18 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_5_OnlineBookReader;
+
+public class Book {
+
+ private int book_id;
+ private String detail;
+
+ public Book(int _book_id, String _detail) {
+ this.book_id = _book_id;
+ this.detail = _detail;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Display.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Display.java
new file mode 100644
index 0000000..fb9df1d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Display.java
@@ -0,0 +1,20 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_5_OnlineBookReader;
+
+public class Display {
+
+ private Book active_book;
+ private User active_user;
+ private long page_num;
+
+ public Display(Book _book, User _user) {
+ this.active_book = _book;
+ this.active_user = _user;
+ page_num = 1;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Library.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Library.java
new file mode 100644
index 0000000..21a1e54
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/Library.java
@@ -0,0 +1,42 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_5_OnlineBookReader;
+
+import CareerCup.ObjectOrientedDesign.Q8_10_HashTable.HashTable;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Iterator;
+
+public class Library {
+
+ private HashTable DB;
+ private static Library inst;
+
+ private Library() {
+ DB = new HashTable<>();
+ }
+
+ public static Library getInstance() {
+ if (inst == null) {
+ inst = new Library();
+ }
+ return inst;
+ }
+
+ public void bulkAddBook(Dictionary list) {
+ Enumeration keys = list.keys();
+ for (; keys.hasMoreElements();) {
+ int id = keys.nextElement();
+ Book book = list.get(id);
+ DB.put(id, book);
+ }
+ }
+
+ public Book find(int _book_id) {
+ return DB.get(_book_id);
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/OnlineReaderSystem.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/OnlineReaderSystem.java
new file mode 100644
index 0000000..ca8eaa7
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/OnlineReaderSystem.java
@@ -0,0 +1,31 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_5_OnlineBookReader;
+
+import java.util.HashSet;
+
+public class OnlineReaderSystem {
+
+ private Library lib;
+ private UserManager manager;
+ private HashSet displays;
+ private static OnlineReaderSystem inst;
+
+ private OnlineReaderSystem() {
+ lib = Library.getInstance();
+ manager = UserManager.getInstance();
+ displays = new HashSet<>();
+ }
+
+ public Display read(User _user, Book _book) {
+ Display d = new Display(_book, _user);
+ if (!displays.contains(d)) {
+ displays.add(d);
+ }
+ return d;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/User.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/User.java
new file mode 100644
index 0000000..7333a86
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/User.java
@@ -0,0 +1,22 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_5_OnlineBookReader;
+
+import java.math.BigInteger;
+
+public class User {
+
+ private BigInteger ID;
+ public String Name;
+ private short Type;
+
+ public User(BigInteger _id, String _name, short _type) {
+ this.ID = _id;
+ this.Name = _name;
+ this.Type = _type;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/UserManager.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/UserManager.java
new file mode 100644
index 0000000..273d34d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_5_OnlineBookReader/UserManager.java
@@ -0,0 +1,40 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_5_OnlineBookReader;
+
+import CareerCup.ObjectOrientedDesign.Q8_10_HashTable.HashTable;
+import java.math.BigInteger;
+
+public class UserManager {
+
+ public HashTable DB;
+ private static UserManager inst;
+
+ private UserManager() {
+ DB = new HashTable<>();
+ entity = BigInteger.ONE;
+ }
+
+ public static UserManager getInstance() {
+ if (inst == null) {
+ inst = new UserManager();
+ }
+ return inst;
+ }
+ static BigInteger entity;
+
+ public void addUser(String _name, short _type) {
+ entity = entity.add(BigInteger.ONE);
+ BigInteger id = entity;
+ User u = new User(id, _name, _type);
+ DB.put(id, u);
+ }
+
+ public void removeUser(BigInteger _id) {
+ DB.delete(_id);
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Edge.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Edge.java
new file mode 100644
index 0000000..0dfe972
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Edge.java
@@ -0,0 +1,26 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_6_Jigsaw;
+
+public class Edge {
+
+ public EdgeType type;
+ public Piece parent;
+
+ public boolean fitWith(Edge e) {
+ if (e.type == EdgeType.flat || this.type == EdgeType.flat) {
+ return false;
+ } else if (e.type == EdgeType.inner) {
+ return this.type == EdgeType.outer;
+ } else if (e.type == EdgeType.outer) {
+ return this.type == EdgeType.inner;
+ } else {
+ return false;
+ }
+ }
+
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/EdgeType.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/EdgeType.java
new file mode 100644
index 0000000..944d896
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/EdgeType.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_6_Jigsaw;
+
+enum EdgeType {
+
+ inner, outer, flat
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Piece.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Piece.java
new file mode 100644
index 0000000..7a56c9c
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Piece.java
@@ -0,0 +1,28 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_6_Jigsaw;
+
+public class Piece {
+
+ Edge[] edges;
+
+ public PieceType getPieceType() {
+ int flat_count = 0;
+ for (int i = 0; i < 4; i++) {
+ if (edges[i].type == EdgeType.flat) {
+ ++flat_count;
+ }
+ }
+ if (flat_count == 2) {
+ return PieceType.corner;
+ } else if (flat_count == 1) {
+ return PieceType.border;
+ } else {
+ return PieceType.inner;
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/PieceType.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/PieceType.java
new file mode 100644
index 0000000..402db8d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/PieceType.java
@@ -0,0 +1,8 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_6_Jigsaw;
+
+enum PieceType{
+ corner, border, inner
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Puzzle.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Puzzle.java
new file mode 100644
index 0000000..d8ad2ce
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_6_Jigsaw/Puzzle.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_6_Jigsaw;
+public class Puzzle {
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Conversation.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Conversation.java
new file mode 100644
index 0000000..c8f1ab4
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Conversation.java
@@ -0,0 +1,23 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+
+public abstract class Conversation {
+
+ ArrayList messages;
+ ArrayList participants;
+ BigInteger id;
+
+ public Conversation(BigInteger _id) {
+ this.id = _id;
+ messages = new ArrayList<>();
+ participants = new ArrayList<>();
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/GroupChat.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/GroupChat.java
new file mode 100644
index 0000000..cc13906
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/GroupChat.java
@@ -0,0 +1,21 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+import java.math.BigInteger;
+
+public class GroupChat extends Conversation {
+
+ public GroupChat(BigInteger _id, User _creater) {
+ super(_id);
+ participants.add(_creater);
+ }
+
+ public void addParticipant(User _participant) {
+ participants.add(_participant);
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Message.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Message.java
new file mode 100644
index 0000000..d49224a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Message.java
@@ -0,0 +1,22 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+import java.util.Date;
+
+public class Message {
+
+ public String message;
+ public Date send_time;
+ public User sender;
+
+ public Message(String _msg, Date _sendtime, User _poster) {
+ this.message = _msg;
+ this.send_time = _sendtime;
+ this.sender = _poster;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/PrivateChat.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/PrivateChat.java
new file mode 100644
index 0000000..ccd0ec4
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/PrivateChat.java
@@ -0,0 +1,18 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+import java.math.BigInteger;
+
+public class PrivateChat extends Conversation {
+
+ public PrivateChat(BigInteger _id, User _from, User _to) {
+ super(_id);
+ participants.add(_from);
+ participants.add(_to);
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Request.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Request.java
new file mode 100644
index 0000000..3eb52ea
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/Request.java
@@ -0,0 +1,21 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+public class Request {
+
+ public User from;
+ public User to;
+ public RequestStatusType status;
+
+ public Request(User _from, User _to) {
+ this.from = _from;
+ this.to = _to;
+ status = RequestStatusType.Unread;
+ }
+
+ public static void main(String[] args) {
+ }
+
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/RequestStatusType.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/RequestStatusType.java
new file mode 100644
index 0000000..e1b7cea
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/RequestStatusType.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+public enum RequestStatusType {
+
+ Approved, Declined, Read, Unread
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/User.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/User.java
new file mode 100644
index 0000000..a0af9fc
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/User.java
@@ -0,0 +1,81 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashSet;
+
+public class User {
+
+ private UserStatus status;
+ private BigInteger id;
+ private String nickname;
+ private String account;
+ public ArrayList contacts;
+ public ArrayList send_list;
+ public ArrayList receive_list;
+ public ArrayList groupchats;
+ public ArrayList privatechats;
+
+ public User(BigInteger _id, String _nickname, String _account) {
+ this.id = _id;
+ this.nickname = _nickname;
+ this.account = _account;
+ status = new UserStatus();
+ contacts = new ArrayList<>();
+ send_list = new ArrayList<>();
+ receive_list = new ArrayList<>();
+ groupchats = new ArrayList<>();
+ privatechats = new ArrayList<>();
+ }
+
+ public BigInteger getId() {
+ return id;
+ }
+
+ public void Speak(String msg, Conversation s) {
+ Date now = new Date();
+ Message m = new Message(msg, now, this);
+ if (s instanceof PrivateChat) {
+ if (s.participants.contains(this) && privatechats.contains(s)) {
+ s.messages.add(m);
+ }
+ } else if (s instanceof GroupChat) {
+ if (groupchats.contains(s)) {
+ s.messages.add(m);
+ }
+ }
+ }
+
+ public void addConversation(Conversation c) {
+ if (c instanceof GroupChat) {
+ groupchats.add((GroupChat) c);
+ } else if (c instanceof PrivateChat) {
+ privatechats.add((PrivateChat) c);
+ }
+ }
+
+ public void sendRequest(User _to) {
+ UserManager.getInstance().sendRequest(this, _to);
+ }
+
+ public void approveRequest(User _to) {
+ UserManager.getInstance().approveRequest(this, _to);
+ }
+
+ public void declineRequest(User _to) {
+ UserManager.getInstance().declineRequest(this, _to);
+ }
+
+ public static void main(String[] args) {
+ User u = new User(BigInteger.ONE, "Sanqiang", "zsq@131x.com");
+ Conversation c = new GroupChat(BigInteger.ONE, u);
+ u.addConversation(c);
+ if (u.groupchats.contains(c)) {
+ System.out.println("Ok");
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserManager.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserManager.java
new file mode 100644
index 0000000..66d783d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserManager.java
@@ -0,0 +1,60 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+import CareerCup.ObjectOrientedDesign.Q8_10_HashTable.HashTable;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Random;
+
+public class UserManager {
+
+ private static UserManager instance = null;
+
+ private UserManager() {
+ db = new HashTable<>();
+ }
+
+ public static UserManager getInstance() {
+ if (instance == null) {
+ instance = new UserManager();
+ }
+ return instance;
+ }
+ HashTable db;
+
+ public void register(String _nickname, String _account) {
+ BigInteger id = BigInteger.probablePrime(10, new Random());
+ User ur = new User(id, _nickname, _account);
+ db.put(id, ur);
+ }
+
+ public void sendRequest(User _from, User _to) {
+ Request r = new Request(_from, _to);
+ _to.receive_list.add(r);
+ }
+
+ public void approveRequest(User _from, User _to) {
+ ArrayList list = _to.receive_list;
+ for (Request request : list) {
+ if (request.from == _from) {
+ request.status = RequestStatusType.Approved;
+ }
+ }
+ _to.contacts.add(_from.getId());
+ _from.contacts.add(_to.getId());
+ }
+
+ public void declineRequest(User _from, User _to) {
+ ArrayList list = _to.receive_list;
+ for (Request request : list) {
+ if (request.from == _from) {
+ request.status = RequestStatusType.Declined;
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserStatus.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserStatus.java
new file mode 100644
index 0000000..2f3816b
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserStatus.java
@@ -0,0 +1,13 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+public class UserStatus {
+
+ public String message;
+ public UserStatusType status;
+
+ public UserStatus() {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserStatusType.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserStatusType.java
new file mode 100644
index 0000000..b40e045
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_ChatServer/UserStatusType.java
@@ -0,0 +1,10 @@
+/**
+ * Sanqiang Zhao Www.131X.Com
+ * Dec 24, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_ChatServer;
+
+enum UserStatusType {
+
+ Online, Offline, Busy, Away, Inavailable
+}
\ No newline at end of file
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Board.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Board.java
new file mode 100644
index 0000000..fcf6d42
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Board.java
@@ -0,0 +1,132 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 23, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_Othello;
+
+public class Board {
+
+ private Piece[][] board;
+ private int height;
+ private int width;
+ int black_score = 0;
+ int white_score = 0;
+
+ public Board(int _column, int _row) {
+ this.board = new Piece[_column][_row];
+ height = _column;
+ width = _row;
+ initiailize();
+ }
+
+ private void initiailize() {
+ board[4][4] = new Piece(Color.Black);
+ board[5][5] = new Piece(Color.Black);
+ board[4][5] = new Piece(Color.White);
+ board[5][4] = new Piece(Color.White);
+ black_score = 2;
+ white_score = 2;
+ }
+
+ public boolean placePiece(int col, int row, Color me) {
+ if (board[col][row] != null) {
+ return false;
+ }
+ int direction[] = new int[4];
+ if (col >= 1 && board[col - 1][row] != null && board[col - 1][row].getColor() != me) {
+ direction[0] = placePieceHelper(col - 1, row, me, Direciton.Up);
+ if (direction[0] > 0) {
+ //board[col - 1][row].flip();
+ }
+
+ }
+ if (col < height - 2 && board[col + 1][row] != null && board[col + 1][row].getColor() != me) {
+ direction[1] = placePieceHelper(col + 1, row, me, Direciton.Down);
+ if (direction[1] > 0) {
+ //board[col + 1][row].flip();
+ }
+ }
+ if (row >= 1 && board[col][row - 1] != null && board[col][row - 1].getColor() != me) {
+ direction[2] = placePieceHelper(col, row - 1, me, Direciton.Left);
+ if (direction[2] > 0) {
+ //board[col][row - 1].flip();
+ }
+ }
+ if (row < width - 2 && board[col][row + 1] != null && board[col][row + 1].getColor() != me) {
+ direction[3] = placePieceHelper(col, row + 1, me, Direciton.Right);
+ if (direction[3] > 0) {
+ //board[col][row + 1].flip();
+ }
+ }
+ int count = 0;
+ for (int i : direction) {
+ count += i;
+ }
+ if (count <= 0) {
+ return false;
+ } else {
+ board[col][row] = new Piece(me);
+ updateScore(me, count);
+ return true;
+ }
+ }
+
+ private int placePieceHelper(int col, int row, Color me, Direciton d) {
+ int h = 0, v = 0;
+ switch (d) {
+ case Up:
+ v = -1;
+ break;
+ case Down:
+ v = 1;
+ break;
+ case Left:
+ h = -1;
+ break;
+ case Right:
+ h = 1;
+ break;
+ }
+ if (board[col][row] == null || board[col][row].getColor() == me) {
+ return 0;
+ }
+ if (col < 0 || row < 0 || col > height - 1 || row > width - 1) {
+ return -1;
+ }
+ int count = placePieceHelper(col + v, row + h, me, d);
+ if (count >= 0) {
+ board[col][row].flip();
+ return 1 + count;
+ } else {
+ return -1;
+ }
+ }
+
+ private void updateScore(Color color, int num) {
+ if (color == Color.Black) {
+ black_score += num + 1;
+ white_score -= num;
+ } else {
+ black_score -= num;
+ white_score += num + 1;
+ }
+ }
+
+ public void print() {
+ for (int col = 0; col < height; col++) {
+ for (int row = 0; row < width; row++) {
+ if (board[col][row] == null) {
+ System.out.print("_");
+ } else if (board[col][row].getColor() == Color.Black) {
+ System.out.print("B");
+ } else if (board[col][row].getColor() == Color.White) {
+ System.out.print("W");
+ }
+ System.out.print(" ");
+ }
+ System.out.println();
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Color.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Color.java
new file mode 100644
index 0000000..596910c
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Color.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 23, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_Othello;
+
+enum Color {
+
+ Black, White
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Direciton.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Direciton.java
new file mode 100644
index 0000000..cdc00e7
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Direciton.java
@@ -0,0 +1,9 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 23, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_Othello;
+
+enum Direciton {
+
+ Up, Down, Left, Right
+}
\ No newline at end of file
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Game.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Game.java
new file mode 100644
index 0000000..743cbb2
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Game.java
@@ -0,0 +1,40 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 23, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_Othello;
+
+public class Game {
+
+ private int column = 10;
+ private int row = 10;
+ private Board board;
+ private static Game instance = null;
+ private Player players[];
+
+ private Game() {
+ board = new Board(column, row);
+ players = new Player[2];
+ players[0] = new Player(Color.Black);
+ players[1] = new Player(Color.White);
+ }
+
+ public static Game getInstance() {
+ if (instance == null) {
+ instance = new Game();
+ }
+ return instance;
+ }
+
+ public Board getBoard() {
+ return board;
+ }
+
+ public static void main(String[] args) {
+ Game g = Game.getInstance();
+ Board b = g.getBoard();
+ b.print();
+ boolean result = b.placePiece(4, 3, Color.White);
+ b.placePiece(5, 6, Color.White);
+ b.print();
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Piece.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Piece.java
new file mode 100644
index 0000000..8e6db3c
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Piece.java
@@ -0,0 +1,32 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 23, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_Othello;
+
+public class Piece {
+
+ private Color color;
+
+ public void setColor(Color _color) {
+ this.color = _color;
+ }
+
+ public Color getColor() {
+ return color;
+ }
+
+ public Piece(Color _color) {
+ this.color = _color;
+ }
+
+ public void flip() {
+ if (this.getColor() == Color.Black) {
+ this.setColor(Color.White);
+ } else {
+ this.setColor(Color.Black);
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Player.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Player.java
new file mode 100644
index 0000000..70cc8d1
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_8_Othello/Player.java
@@ -0,0 +1,16 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 23, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_8_Othello;
+
+public class Player {
+
+ public Color flag;
+
+ public Player(Color _color) {
+ this.flag = _color;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/Directory.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/Directory.java
new file mode 100644
index 0000000..fe4c66d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/Directory.java
@@ -0,0 +1,56 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 19, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_9_FileSystem;
+
+import java.util.HashSet;
+
+public class Directory extends Entry {
+
+ public HashSet files;
+
+ public Directory(String _name, Directory _parent) {
+ super(_name, _parent);
+ files = new HashSet<>();
+ }
+
+ @Override
+ public int size() {
+ int size = 0;
+ for (Entry e : files) {
+ size += e.size();
+ }
+ return size;
+ }
+
+ @Override
+ public void delete() {
+ for (Entry e : files) {
+ e.delete();
+ }
+ this.remove = true;
+ }
+
+ public void deleteEntry(Entry e) {
+ files.remove(e);
+ }
+
+ public void addEntry(Entry e) {
+ files.add(e);
+ }
+
+ public int numfiles() {
+ int count = 0;
+ for (Entry e : files) {
+ if (e instanceof File) {
+ ++count;
+ } else if (e instanceof Directory) {
+ count += ((Directory) e).numfiles();
+ }
+ }
+ return count;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/Entry.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/Entry.java
new file mode 100644
index 0000000..e816c1a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/Entry.java
@@ -0,0 +1,36 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 19, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_9_FileSystem;
+
+public abstract class Entry {
+
+ public long date_create;
+ public long date_update;
+ public long date_access;
+ public String name;
+ public Directory parent;
+ public boolean remove;
+
+ public Entry(String _name, Directory _parent) {
+ this.name = _name;
+ this.parent = _parent;
+ date_create = System.currentTimeMillis();
+ date_update = System.currentTimeMillis();
+ date_access = System.currentTimeMillis();
+ remove = false;
+ }
+
+ public void deleteParent() {
+ if (parent != null) {
+ parent.delete();
+ }
+ }
+
+ public abstract int size();
+
+ public abstract void delete();
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/File.java b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/File.java
new file mode 100644
index 0000000..56b6a60
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ObjectOrientedDesign/Q8_9_FileSystem/File.java
@@ -0,0 +1,30 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 19, 2012
+ */
+package CareerCup.ObjectOrientedDesign.Q8_9_FileSystem;
+
+public class File extends Entry {
+
+ public String content;
+ public int size;
+
+ public File(String _name, Directory _parent, String _content) {
+ super(_name, _parent);
+ this.content = _content;
+ this.size = _content.length();
+ }
+
+ @Override
+ public int size() {
+ return size;
+ }
+
+ @Override
+ public void delete() {
+ parent.files.remove(this);
+ this.remove = true;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_10_CreateStackBox.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_10_CreateStackBox.java
new file mode 100644
index 0000000..3e8792e
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_10_CreateStackBox.java
@@ -0,0 +1,70 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+public class Q9_10_CreateStackBox {
+
+ class Box {
+
+ public int width;
+ public int height;
+ public int depth;
+
+ public boolean canBeAbove(Box another) {
+ if (another == null) {
+ return true;
+ } else {
+ return this.width < another.width && this.height < another.height && this.depth < another.depth;
+ }
+ }
+
+ public void print() {
+ System.out.append("W:" + width + " H:" + height + " D:" + depth);
+ }
+ }
+
+ private int getHeight(ArrayList list) {
+ if (list == null) {
+ return 0;
+ }
+ int height = 0;
+ for (Box box : list) {
+ height += box.height;
+ }
+ return height;
+ }
+ HashMap> cache = new HashMap<>();
+
+ public ArrayList getLongest(Box[] boxes, Box bottom) {
+ if (bottom != null && cache.containsKey(bottom)) {
+ return cache.get(bottom);
+ }
+ ArrayList solution = null;
+ int maxheight = 0;
+ for (Box box : boxes) {
+ if (box.canBeAbove(bottom)) {
+ ArrayList temp_solution = getLongest(boxes, box);
+ int temp_height = getHeight(temp_solution);
+ if (temp_height > maxheight) {
+ maxheight = temp_height;
+ solution = temp_solution;
+ }
+ }
+ }
+ if (bottom != null) {
+ solution.add(0, bottom);
+ }
+ if (solution == null) {
+ solution = new ArrayList<>();
+ }
+ cache.put(bottom, solution);
+ return solution;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_11_BinaryRepresentation.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_11_BinaryRepresentation.java
new file mode 100644
index 0000000..9cb043a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_11_BinaryRepresentation.java
@@ -0,0 +1,67 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+import java.util.HashMap;
+import sun.security.krb5.internal.KDCOptions;
+
+public class Q9_11_BinaryRepresentation {
+
+ HashMap cache = new HashMap<>();
+
+ public int f(String text, int s, int e, boolean result) {
+ String key = String.valueOf(s) + (result ? "y" : "n") + String.valueOf(e);
+ if (cache.containsKey(key)) {
+ return cache.get(key);
+ }
+ if (s == e) {
+ if ((text.charAt(s) == '1' && result) || (text.charAt(s) == '0' && !result)) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ int ways = 0;
+ if (result) {
+ for (int m = s + 1; m <= e; m += 2) {
+ switch (text.charAt(m)) {
+ case '&':
+ ways += f(text, s, m - 1, true) * f(text, m + 1, e, true);
+ break;
+ case '|':
+ ways += f(text, s, m - 1, true) * f(text, m + 1, e, false)
+ + f(text, s, m - 1, false) * f(text, m + 1, e, true)
+ + f(text, s, m - 1, true) * f(text, m + 1, e, true);
+ break;
+ case '^':
+ ways += f(text, s, m - 1, true) * f(text, m + 1, e, false)
+ + f(text, s, m - 1, false) * f(text, m + 1, e, true);
+ break;
+ }
+ }
+ } else {
+ for (int m = s + 1; m <= e; m += 2) {
+ switch (text.charAt(m)) {
+ case '&':
+ ways += f(text, s, m - 1, false) * f(text, m + 1, e, false)
+ + f(text, s, m - 1, false) * f(text, m + 1, e, true)
+ + f(text, s, m - 1, true) * f(text, m + 1, e, true);
+ break;
+ case '|':
+ ways += f(text, s, m - 1, false) * f(text, m + 1, e, false);
+ break;
+ case '^':
+ ways += f(text, s, m - 1, true) * f(text, m + 1, e, true)
+ + f(text, s, m - 1, false) * f(text, m + 1, e, false);
+ break;
+ }
+ }
+ }
+ cache.put(key, ways);
+ return ways;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_1_CountWays.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_1_CountWays.java
new file mode 100644
index 0000000..d330ca0
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_1_CountWays.java
@@ -0,0 +1,32 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+import java.util.HashMap;
+
+public class Q9_1_CountWays {
+
+ private HashMap cache = new HashMap<>();
+
+ public int count(int n) {
+ if (n == 0) {
+ return 1;
+ } else if (n < 0) {
+ return 0;
+ }
+ if (cache.containsKey(n)) {
+ return cache.get(n);
+ }
+ int ways = count(n - 1) + count(n - 2) + count(n - 3);
+ cache.put(n, ways);
+ return ways;
+ }
+
+ public static void main(String[] args) {
+ Q9_1_CountWays q91 = new Q9_1_CountWays();
+ int ways = q91.count(3);
+ System.out.println(ways);
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_2_GetRobotPath.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_2_GetRobotPath.java
new file mode 100644
index 0000000..870d8e9
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_2_GetRobotPath.java
@@ -0,0 +1,62 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+public class Q9_2_GetRobotPath {
+
+ private HashMap cache = new HashMap<>();
+ public boolean isConnected(int x, int y, ArrayList path) {
+ boolean result = false;
+ Point p = new Point(x, y);
+ path.add(p);
+ if (cache.containsKey(p)) {
+ return cache.get(p);
+ }
+ if (x == 0 && y == 0) {
+ return true;
+ }
+ if (x > 0 && isfree(x - 1, y)) {
+ result = isConnected(x - 1, y, path);
+ }
+ if (result) {
+ return true;
+ }
+ if (y > 0 && isfree(x, y - 1)) {
+ result = isConnected(x, y - 1, path);
+ }
+ if (!result) {
+ path.remove(p);
+ }
+ cache.put(p, result);
+ return result;
+ }
+
+ boolean isfree(int x, int y) {
+ return true;
+ }
+
+ class Point {
+
+ int x;
+ int y;
+
+ public Point(int _x, int _y) {
+ this.x = _x;
+ this.y = _y;
+ }
+ }
+
+ public static void main(String[] args) {
+ Q9_2_GetRobotPath q92 = new Q9_2_GetRobotPath();
+ ArrayList path = new ArrayList<>();
+ boolean result = q92.isConnected(41, 51, path);
+ System.out.println(result);
+ for (Point point : path) {
+ System.out.println(point.x + "," + point.y);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_3_MagicIndex.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_3_MagicIndex.java
new file mode 100644
index 0000000..b16993d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_3_MagicIndex.java
@@ -0,0 +1,50 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+public class Q9_3_MagicIndex {
+
+ public int find(int[] array) {
+ int s = 0, length = array.length, e = length - 1;
+ while (s <= e) {
+ int m = s + (e - s) / 2;
+ if (m == array[m]) {
+ return m;
+ } else if (m > array[m]) {
+ s = m + 1;
+ } else {
+ e = m - 1;
+ }
+ }
+ return -1;
+ }
+
+ public int findWithOverlap(int[] array, int s, int e) {
+ if (s > e) {
+ return -1;
+ }
+ int m = s + (e - s) / 2;
+ if (m == array[m]) {
+ return m;
+ }
+ int l = Math.min(array[m], m - 1);
+ int result = findWithOverlap(array, s, l);
+ if (result != -1) {
+ return result;
+ }
+ int h = Math.max(array[m], m + 1);
+ result = findWithOverlap(array, h, e);
+ return result;
+ }
+
+ public static void main(String[] args) {
+ Q9_3_MagicIndex q93 = new Q9_3_MagicIndex();
+ int[] arr1 = {-40, -20, -1, 1, 2, 3, 5, 7, 9, 12, 13};
+ int i1 = q93.find(arr1);
+ System.out.println(i1);
+ int[] arr2 = {-10, -5, 2, 2, 2, 3, 4, 8, 9, 12, 13};
+ int i2 = q93.findWithOverlap(arr2, 0, arr2.length - 1);
+ System.out.println(i2);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_4_Subsets.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_4_Subsets.java
new file mode 100644
index 0000000..115e685
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_4_Subsets.java
@@ -0,0 +1,73 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+import java.util.ArrayList;
+
+public class Q9_4_Subsets {
+
+ public ArrayList> getSubsets2(ArrayList set) {
+ ArrayList> solution = new ArrayList<>();
+ int length = set.size(), subset_size = 1 << length;
+ for (int i = 0; i < subset_size; i++) {
+ ArrayList temp = new ArrayList<>();
+ for (int pos = 0; pos < length; pos++) {
+ if (((1 << pos) & i) > 0) {
+ temp.add(set.get(pos));
+ }
+ }
+ solution.add(temp);
+ }
+ return solution;
+ }
+
+ public ArrayList> getSubsets(ArrayList set, int index) {
+ if (index == set.size()) {
+ ArrayList> solution = new ArrayList<>();
+ ArrayList empty = new ArrayList<>();
+ solution.add(empty);
+ return solution;
+ }
+ ArrayList> solution = getSubsets(set, index + 1);
+ char ch = set.get(index);
+ ArrayList> temp = new ArrayList<>();
+ for (ArrayList arrayList : solution) {
+ ArrayList temp_item = new ArrayList<>();
+ temp_item.addAll(arrayList);
+ temp_item.add(ch);
+ temp.add(temp_item);
+ }
+ solution.addAll(temp);
+ return solution;
+ }
+
+ public static void main(String[] args) {
+ ArrayList set = new ArrayList<>();
+ String text = Util.TestUtil.generateArrayString(20, 9);
+ for (Character ch : text.toCharArray()) {
+ set.add(ch);
+ }
+ Q9_4_Subsets q94 = new Q9_4_Subsets();
+ long t1 = System.currentTimeMillis();
+ ArrayList> solution1 = q94.getSubsets(set, 0);
+ //print(solution1);
+ long t2 = System.currentTimeMillis();
+ ArrayList> solution2 = q94.getSubsets2(set);
+ //print(solution2);
+ long t3 = System.currentTimeMillis();
+ long sp1 = t2 - t1, sp2 = t3 - t2;
+ System.out.println(solution1.size() + " Cost " + sp1);
+ System.out.println(solution2.size() + " Cost " + sp2);
+ }
+
+ static void print(ArrayList> solution) {
+ for (ArrayList arrayList : solution) {
+ for (Character character : arrayList) {
+ System.out.print(character);
+ }
+ System.out.println();
+ }
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_5_Permutation.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_5_Permutation.java
new file mode 100644
index 0000000..13b0c9b
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_5_Permutation.java
@@ -0,0 +1,45 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+import java.util.ArrayList;
+
+public class Q9_5_Permutation {
+
+ public ArrayList getPermuatation(String text) {
+ if (text.length() == 1) {
+ ArrayList solution = new ArrayList<>();
+ solution.add(text);
+ return solution;
+ }
+ String current = text.substring(0, 1);
+ String remaining = text.substring(1);
+ ArrayList solution = getPermuatation(remaining);
+ ArrayList next_solution = new ArrayList<>();
+ for (int i = 0; i < solution.size(); i++) {
+ ArrayList temp = inject(solution.get(i), current);
+ next_solution.addAll(temp);
+ }
+ return next_solution;
+ }
+
+ private ArrayList inject(String text, String ch) {
+ ArrayList solution = new ArrayList<>();
+ for (int i = 0; i <= text.length(); i++) {
+ String left = text.substring(0, i);
+ String right = text.substring(i);
+ solution.add(left + ch + right);
+ }
+ return solution;
+ }
+
+ public static void main(String[] args) {
+ String text = "abc";
+ Q9_5_Permutation q95 = new Q9_5_Permutation();
+ ArrayList solution = q95.getPermuatation(text);
+ for (String string : solution) {
+ System.out.println(string);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_6_ValidParentheses.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_6_ValidParentheses.java
new file mode 100644
index 0000000..293ad13
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_6_ValidParentheses.java
@@ -0,0 +1,39 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+public class Q9_6_ValidParentheses {
+
+ public void printParenthesis(int pair) {
+ boolean[] presentation = new boolean[pair * 2];
+ printParentheses(pair, pair, presentation, 0);
+ }
+
+ private void printParentheses(int left, int right, boolean[] presentation, int index) {
+ if (left == 0 && right == 0) {
+ for (int i = 0; i < presentation.length; i++) {
+ if (presentation[i]) {
+ System.out.print("(");
+ } else {
+ System.out.print(")");
+ }
+ }
+ System.out.println();
+ }
+ if (left > 0) {
+ presentation[index] = true;
+ printParentheses(left - 1, right, presentation, index + 1);
+ }
+
+ if (left < right) {
+ presentation[index] = false;
+ printParentheses(left, right - 1, presentation, index + 1);
+ }
+ }
+
+ public static void main(String[] args) {
+ Q9_6_ValidParentheses q96 = new Q9_6_ValidParentheses();
+ q96.printParenthesis(3);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_7_PaintFill.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_7_PaintFill.java
new file mode 100644
index 0000000..cdc180e
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_7_PaintFill.java
@@ -0,0 +1,27 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+public class Q9_7_PaintFill {
+
+ enum Color {
+
+ Red, Green, Yellow
+ }
+
+ public void paint(Color[][] canvas, int col, int row, Color ncolor, Color ocolor) {
+ int height = canvas.length, width = canvas[0].length;
+ if (col < 0 || col >= height || row < 0 || row >= width || canvas[col][row] != ocolor) {
+ return;
+ }
+ canvas[col][row] = ncolor;
+ paint(canvas, col - 1, row, ncolor, ocolor);
+ paint(canvas, col + 1, row, ncolor, ocolor);
+ paint(canvas, col, row - 1, ncolor, ocolor);
+ paint(canvas, col, row + 1, ncolor, ocolor);
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_8_CountCoin.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_8_CountCoin.java
new file mode 100644
index 0000000..b599421
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_8_CountCoin.java
@@ -0,0 +1,46 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+import java.util.HashMap;
+
+public class Q9_8_CountCoin {
+
+ HashMap cache = new HashMap<>();
+
+ public int count(int charge, int coin) {
+ if (cache.containsKey(charge)) {
+ //System.out.println(charge + ":" + cache.get(charge));
+ //return cache.get(charge);
+ }
+ int ways = 0;
+ int next_coin = 0;
+ switch (coin) {
+ case 25:
+ next_coin = 10;
+ break;
+ case 10:
+ next_coin = 5;
+ break;
+ case 5:
+ next_coin = 1;
+ break;
+ case 1:
+ return 1;
+ default:
+ return 0;
+ }
+ for (int i = 0; charge - i * coin >= 0; i++) {
+ ways += count(charge - i * coin, next_coin);
+ }
+ //cache.put(charge, ways);
+ return ways;
+ }
+
+ public static void main(String[] args) {
+ Q9_8_CountCoin q98 = new Q9_8_CountCoin();
+ int ways = q98.count(50, 25);
+ System.out.println(ways);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_9_PlaceQueen.java b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_9_PlaceQueen.java
new file mode 100644
index 0000000..e2eddc5
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/RecursiveAndDP/Q9_9_PlaceQueen.java
@@ -0,0 +1,59 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.RecursiveAndDP;
+
+public class Q9_9_PlaceQueen {
+
+ final int GRID_SIZE = 8;
+
+ public void place() {
+ place(0, new int[GRID_SIZE]);
+ }
+
+ public void place(int row, int[] chess) {
+ if (row == GRID_SIZE) {
+ print(chess);
+ return;
+ }
+
+ for (int i = 0; i < GRID_SIZE; i++) {
+ chess[row] = i;
+ if (checkValid(row, chess)) {
+ place(row + 1, chess);
+ }
+ }
+ }
+
+ private boolean checkValid(int row, int[] chess) {
+ for (int i = 0; i < row; i++) {
+ int w = Math.abs(chess[i] - chess[row]);
+ int h = Math.abs(i - row);
+ if (w == 0 || w == h) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private void print(int[] chess) {
+ for (int i = 0; i < GRID_SIZE; i++) {
+ int col = chess[i];
+ int j = 0;
+ for (; j < col; j++) {
+ System.out.print("_");
+ }
+ System.out.print("*");
+ for (j = col + 1; j < GRID_SIZE; j++) {
+ System.out.print("_");
+ }
+ System.out.println();
+ }
+ System.out.println();
+ }
+
+ public static void main(String[] args) {
+ Q9_9_PlaceQueen q99 = new Q9_9_PlaceQueen();
+ q99.place();
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ScalabilityAndMemoryLimits/Q10_3_4_BitSet/BitSet.java b/Algorithm-Java-Backup/src/CareerCup/ScalabilityAndMemoryLimits/Q10_3_4_BitSet/BitSet.java
new file mode 100644
index 0000000..569af44
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ScalabilityAndMemoryLimits/Q10_3_4_BitSet/BitSet.java
@@ -0,0 +1,43 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 28, 2012
+ */
+package CareerCup.ScalabilityAndMemoryLimits.Q10_3_4_BitSet;
+
+public class BitSet {
+
+ long[] buffer;
+
+ public BitSet(int _size) {
+ if (_size >> 10 == 0) {
+ this.buffer = new long[1];
+ } else {
+ this.buffer = new long[_size >> 10];
+ }
+ }
+
+ public BitSet(int _size, boolean buffer_size) {
+ if (buffer_size) {
+ this.buffer = new long[_size];
+ } else {
+ //
+ }
+ }
+
+ public void set(int n) {
+ int word_num = n >> 10; //divid by 32
+ int bit_num = n & 0x3FF; //mod 32
+ buffer[word_num] |= 1 << bit_num;
+ }
+
+ public boolean get(int n) {
+ int word_num = n >> 10;
+ int bit_num = n & 0x3FF;
+ return (buffer[word_num] & 1 << bit_num) != 0;
+ }
+
+ public static void main(String[] args) {
+ BitSet bs = new BitSet(5);
+ bs.set(3);
+ System.out.println(bs.get(0));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/HeapSort.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/HeapSort.java
new file mode 100644
index 0000000..ce3dcfe
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/HeapSort.java
@@ -0,0 +1,56 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.SortAndSearch;
+
+public class HeapSort> extends SortBase {
+
+ public HeapSort(T[] _array) {
+ super(_array);
+ }
+
+ public int getLeft(int index) {
+ return index * 2 + 1;
+ }
+
+ public int getRight(int index) {
+ return index * 2 + 2;
+ }
+
+ public void maxHeapify(int index, int size) {
+ int l = getLeft(index);
+ int r = getRight(index);
+ int MaxIndex = index;
+ if (l < size && array[l].compareTo(array[MaxIndex]) > 0) {
+ MaxIndex = l;
+ }
+ if (r < size && array[r].compareTo(array[MaxIndex]) > 0) {
+ MaxIndex = r;
+ }
+ if (MaxIndex != index) {
+ swap(MaxIndex, index);
+ maxHeapify(MaxIndex, size);
+ }
+ }
+
+ @Override
+ void sort() {
+ int i, length = array.length;
+ for (i = length / 2 - 1; i >= 0; --i) {
+ maxHeapify(i, length);
+ }
+ for (i = length - 1; i >= 0; --i) {
+ swap(i, 0);
+ maxHeapify(0, i);
+ }
+ }
+
+ public static void main(String[] args) {
+ Integer[] arr = {8, 6, 4, 2, 3};
+ HeapSort hs = new HeapSort<>(arr);
+ hs.sort();
+ for (Integer integer : hs.array) {
+ System.out.println(integer);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/InsertSort.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/InsertSort.java
new file mode 100644
index 0000000..a6bda21
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/InsertSort.java
@@ -0,0 +1,44 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.SortAndSearch;
+
+public class InsertSort> extends SortBase {
+
+ public InsertSort(T[] _array) {
+ super(_array);
+ }
+
+ @Override
+ void sort() {
+ int i, j, length = array.length;
+ for (i = 0; i < length; ++i) {
+ for (j = i; j - 1 >= 0 && array[j].compareTo(array[j - 1]) < 0; --j) {
+ swap(j, j - 1);
+ }
+ }
+ }
+ final int[] Marcin_Ciura_Gap = {701, 301, 132, 57, 23, 10, 4, 1};
+
+ void sort_shell() {
+ int i, j, length = array.length;
+ for (int step : Marcin_Ciura_Gap) {
+ for (i = 0; i < length; i += step) {
+ for (j = i; j - step >= 0 && array[j].compareTo(array[j - step]) < 0; j -= step) {
+ swap(j, j - step);
+ }
+ }
+ }
+
+ }
+
+ public static void main(String[] args) {
+ Integer[] arr = {8, 6, 4, 2, 3, 0, 2, 10, 12};
+ InsertSort is = new InsertSort<>(arr);
+ //is.sort();
+ is.sort_shell();
+ for (Integer integer : is.array) {
+ System.out.println(integer);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/MergeSort.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/MergeSort.java
new file mode 100644
index 0000000..8b63791
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/MergeSort.java
@@ -0,0 +1,74 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.SortAndSearch;
+
+public class MergeSort> extends SortBase {
+
+ public MergeSort(T[] _array) {
+ super(_array);
+ }
+
+ @Override
+ void sort() {
+ split(0, array.length - 1);
+ }
+
+ void split(int s, int e) {
+ if (s >= e) {
+ return;
+ }
+ int m = s + (e - s) / 2;
+ split(s, m);
+ split(m + 1, e);
+ merge(s, e, m);
+ }
+
+ //Java didnot support generic array creation
+ void merge(int s, int e, int m) {
+ Object[] ll = new Object[m - s + 1];
+ Object[] rr = new Object[e - (m + 1) + 1];
+ int i, j, k;
+ for (i = 0; i < ll.length; i++) {
+ ll[i] = array[s + i];
+ }
+ for (i = 0; i < rr.length; i++) {
+ rr[i] = array[m + i + 1];
+ }
+ i = 0;
+ j = 0;
+ k = s;
+ while (true) {
+ if (i == ll.length) {
+ while (j != rr.length) {
+ array[k++] = (T) rr[j++];
+ }
+ break;
+ }
+ if (j == rr.length) {
+ while (i != ll.length) {
+ array[k++] = (T) ll[i++];
+ }
+ break;
+ }
+ T l = (T) ll[i];
+ T r = (T) rr[j];
+ if (l.compareTo(r) <= 0) {
+ array[k++] = l;
+ i++;
+ } else {
+ array[k++] = r;
+ j++;
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ Integer[] arr = {8, 6, 4, 2, 3};
+ MergeSort ms = new MergeSort<>(arr);
+ ms.sort();
+ for (Integer integer : ms.array) {
+ System.out.println(integer);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/NthBiggest.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/NthBiggest.java
new file mode 100644
index 0000000..aca4998
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/NthBiggest.java
@@ -0,0 +1,92 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.SortAndSearch;
+
+import java.util.Random;
+
+public class NthBiggest> extends SortBase {
+
+ int k;
+
+ public NthBiggest(T[] _array, int _k) {
+ super(_array);
+ this.k = _k;
+ }
+
+ @Override
+ void sort() {
+ int i, length = array.length;
+ for (i = length / 2 - 1; i >= 0; --i) {
+ maxHeapify(i, length);
+ }
+ for (i = length - 1; i > length - 1 - k; --i) {
+ swap(0, i);
+ maxHeapify(0, i);
+ }
+ }
+
+ void sort2() {
+ sort_selection(0, array.length - 1, k);
+ }
+
+ void sort_selection(int s, int e, int k) {
+ int random = new Random().nextInt(e - s + 1) + s;
+ T pivot = array[random];
+ int LeftEnd = selection_partition(s, e, pivot);
+ int LeftSize = LeftEnd - s + 1;
+ if (LeftSize == k) {
+ return;
+ } else if (LeftSize < k) {
+ sort_selection(LeftEnd + 1, e, k - LeftSize);
+ } else {
+ sort_selection(s, LeftEnd, k);
+ }
+
+ }
+
+ int selection_partition(int s, int e, T pivot) {
+ while (true) {
+ for (; s <= e && array[s].compareTo(pivot) <= 0; ++s);
+ for (; s <= e && array[e].compareTo(pivot) >= 0; --e);
+ if (s > e) {
+ return s - 1;
+ }
+ swap(s, e);
+ }
+ }
+
+ private int getLeft(int index) {
+ return index * 2 + 1;
+ }
+
+ private int getRight(int index) {
+ return index * 2 + 2;
+ }
+
+ public void maxHeapify(int index, int size) {
+ int l = getLeft(index);
+ int r = getRight(index);
+ int MaxIndex = index;
+ if (l < size && array[l].compareTo(array[MaxIndex]) > 0) {
+ MaxIndex = l;
+ }
+ if (r < size && array[r].compareTo(array[MaxIndex]) > 0) {
+ MaxIndex = r;
+ }
+ if (MaxIndex != index) {
+ swap(index, MaxIndex);
+ maxHeapify(MaxIndex, size);
+ }
+ }
+
+ public static void main(String[] args) {
+ Integer[] arr = {10,0, 9, 3, 8, 1,7, 6};
+ NthBiggest nb = new NthBiggest<>(arr, 2);
+ //nb.sort();
+ nb.sort2();;
+ for (Integer integer : nb.array) {
+ System.out.println(integer);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_1_Merge.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_1_Merge.java
new file mode 100644
index 0000000..ba98e68
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_1_Merge.java
@@ -0,0 +1,31 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.SortAndSearch;
+
+public class Q11_1_Merge {
+
+ public void merge(int[] large, int[] small, int len_large, int len_small) {
+ int index_large = len_large - 1;
+ int index_small = len_small - 1;
+ int index = large.length - 1;
+ while (true) {
+ if (index_large < 0) {
+ while (index_small >= 0) {
+ large[index--] = small[index_small--];
+ }
+ return;
+ } else if (index_small < 0) {
+ return;
+ }
+ if (large[index_large] <= small[index_small]) {
+ large[index--] = small[index_small--];
+ } else {
+ large[index--] = large[index_large--];
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_2_SortStrings.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_2_SortStrings.java
new file mode 100644
index 0000000..a57af81
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_2_SortStrings.java
@@ -0,0 +1,53 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.SortAndSearch;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+public class Q11_2_SortStrings {
+
+ public void sort(String[] list) {
+ Arrays.sort(list, new comp());
+ }
+
+ public class comp implements Comparator {
+
+ @Override
+ public int compare(String t, String t1) {
+ if (isAnagram(t, t1)) {
+ return 0;
+ } else {
+ return t.hashCode() - t1.hashCode();
+ }
+ }
+
+ private boolean isAnagram(String t, String t1) {
+ int t_length = t.length(), t1_length = t1.length(), i;
+ if (t_length != t1_length) {
+ return false;
+ }
+ int[] tab = new int[0xff];
+ for (i = 0; i < t_length; i++) {
+ ++tab[t.charAt(i)];
+ }
+ for (i = 0; i < t1_length; i++) {
+ --tab[t1.charAt(i)];
+ if (tab[t1.charAt(i)] < 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+
+ public static void main(String[] args) {
+ String[] list = {"op", "d", "abb", "po", "bab", "po", "ki"};
+ Q11_2_SortStrings q112 = new Q11_2_SortStrings();
+ q112.sort(list);
+ for (String string : list) {
+ System.out.println(string);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_3_SortRotatedArray.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_3_SortRotatedArray.java
new file mode 100644
index 0000000..bf24f25
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_3_SortRotatedArray.java
@@ -0,0 +1,44 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.SortAndSearch;
+
+public class Q11_3_SortRotatedArray {
+
+ public int find(int[] arr, int s, int e, int target) {
+ if (s > e) {
+ return -1;
+ }
+ int m = s + (e - s) / 2;
+ if (arr[m] == target) {
+ return m;
+ }
+ if (arr[s] < arr[m]) {
+ if (target >= arr[s] && target <= arr[m]) {
+ return find(arr, s, m - 1, target);
+ } else {
+ return find(arr, m + 1, e, target);
+ }
+ } else if (arr[m] < arr[e]) {
+ if (target >= arr[m] && target <= arr[e]) {
+ return find(arr, m + 1, e, target);
+ } else {
+ return find(arr, s, m - 1, target);
+ }
+ } else {
+ if (arr[m] != arr[e]) {
+ return find(arr, m + 1, e, target);
+ } else {
+ int f = find(arr, m + 1, e, target);
+ if (f == -1) {
+ return find(arr, s, m + 1, target);
+ } else {
+ return f;
+ }
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_5_FindString.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_5_FindString.java
new file mode 100644
index 0000000..0bfe67a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_5_FindString.java
@@ -0,0 +1,35 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.SortAndSearch;
+
+public class Q11_5_FindString {
+
+ public int find(String[] list, String target, int s, int e) {
+ if (s > e) {
+ return -1;
+ }
+ int m = s + (e - s) / 2;
+ if (list[m].length() == 0) {
+ int dif = 1;
+ while (m + dif <= e && m - dif >= s) {
+ if (list[m + dif].length() != 0) {
+ m = m + dif;
+ } else if (list[m - dif].length() != 0) {
+ m = m - dif;
+ }
+ ++dif;
+ }
+ }
+ if (target.compareTo(list[m]) > 0) {
+ return find(list, target, m + 1, e);
+ } else if (target.compareTo(list[m]) < 0) {
+ return find(list, target, s, m - 1);
+ } else {
+ return m;
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_6_FindMatrix.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_6_FindMatrix.java
new file mode 100644
index 0000000..6306aae
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_6_FindMatrix.java
@@ -0,0 +1,149 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.SortAndSearch;
+
+public class Q11_6_FindMatrix {
+
+ public void find2(int[][] matrix, int target) {
+ int height = matrix.length, width = matrix[0].length;
+ int col = 0, row = width - 1;
+ while (row >= 0 && col < height) {
+ if (matrix[col][row] == target) {
+ System.out.println(col + "," + row);
+ return;
+ } else if (matrix[col][row] < target) {
+ ++col;
+ } else {
+ --row;
+ }
+ }
+ System.out.println("Not Find!!!");
+ }
+
+ public void find1(int[][] matrix, int target) {
+ int height = matrix.length, width = matrix[0].length;
+ Point e = new Point(height - 1, width - 1);
+ Point s = new Point(0, 0);
+ Point result = find1(matrix, target, s, e);
+ if (result == null) {
+ System.out.println("Not Find!!!");
+ } else {
+ System.out.println(result.y + "," + result.x);
+ }
+ }
+
+ public Point find1(int[][] matrix, int target, Point s, Point e) {
+ if (s.x > e.x || s.y > e.y) {
+ return null;
+ }
+ Point pivot = findPivot(matrix, target, s, e);
+ if (pivot.inBound(matrix) && matrix[pivot.y][pivot.x] == target) {
+ return pivot;
+ }
+ Point left_top = new Point(pivot.y, s.x);
+ Point left_bottom = new Point(e.y, pivot.x - 1);
+ Point right_top = new Point(s.y, pivot.x);
+ Point right_bottom = new Point(pivot.y - 1, e.x);
+ Point p = find1(matrix, target, left_top, left_bottom);
+ if (p == null) {
+ return find1(matrix, target, right_top, right_bottom);
+ } else {
+ return p;
+ }
+ }
+
+ public Point findPivot(int[][] matrix, int target, Point s, Point e) {
+ if (s.x > e.x || s.y > e.y || !s.inBound(matrix) || !e.inBound(matrix)) {
+ return null;
+ }
+ Point ss = s.clone();
+ int dif = Math.min(e.x - s.x, e.y - s.y);
+ Point ee = new Point(ss.y + dif, ss.x + dif);
+ Point m = null;
+ while (ss.lessThanEqualto(ee)) {
+ m = ss.getMedian(ee);
+ if (matrix[m.y][m.x] == target) {
+ return m;
+ } else if (matrix[m.y][m.x] < target) {
+ //ss.increment();
+ ss.x = m.x + 1;
+ ss.y = m.y + 1;
+ } else {
+ ee.x = m.x - 1;
+ ee.y = m.y - 1;
+ //ee.decrement();
+ }
+ }
+ return ss;
+ }
+
+ class Point {
+
+ public int x;
+ public int y;
+
+ public Point() {
+ }
+
+ public Point(int _y, int _x) {
+ this.x = _x;
+ this.y = _y;
+ }
+
+ public void increment() {
+ ++this.x;
+ ++this.y;
+ }
+
+ public void decrement() {
+ --this.x;
+ --this.y;
+ }
+
+ public boolean lessThan(Point p) {
+ return this.x < p.x && this.y < p.y;
+ }
+
+ public boolean moreThan(Point p) {
+ return this.x > p.x && this.y > p.y;
+ }
+
+ public boolean lessThanEqualto(Point p) {
+ return this.x <= p.x && this.y <= p.y;
+ }
+
+ public boolean moreThanEqualto(Point p) {
+ return this.x >= p.x && this.y >= p.y;
+ }
+
+ public Point getMedian(Point p) {
+ return new Point((this.y + p.y) / 2, (this.x + p.x) / 2);
+ }
+
+ public boolean inBound(int[][] matrix) {
+ int height = matrix.length, width = matrix[0].length;
+ if (this.x >= 0 && this.x < width && this.y >= 0 && this.y < height) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ protected Point clone() {
+ return new Point(y, x);
+ }
+ }
+
+ public static void main(String[] args) {
+ int[][] matrix = {
+ {15, 20, 70, 85},
+ {20, 35, 80, 95},
+ {30, 55, 95, 105},
+ {40, 80, 120, 120}
+ };
+ Q11_6_FindMatrix q116 = new Q11_6_FindMatrix();
+ q116.find1(matrix, 20);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_7_LongestSubsequence.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_7_LongestSubsequence.java
new file mode 100644
index 0000000..346206d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_7_LongestSubsequence.java
@@ -0,0 +1,76 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.SortAndSearch;
+
+import java.util.ArrayList;
+
+public class Q11_7_LongestSubsequence {
+
+ public ArrayList find(ArrayList items) {
+ int length = items.size();
+ ArrayList[] solutions = new ArrayList[length];
+ ArrayList best = null;
+ for (ArrayList list : solutions) {
+ best = setBest(list, best);
+ }
+ return best;
+ }
+
+ public void findDP(ArrayList[] solutions, ArrayList items, int index) {
+ if (index == solutions.length) {
+ return;
+ }
+ HtWt cur = items.get(index);
+ ArrayList cur_best = null;
+ for (int i = 0; i < index; i++) {
+ ArrayList pre_best = solutions[i];
+ cur_best = setBest(pre_best, cur_best);
+ }
+ ArrayList solution = new ArrayList<>();
+ if (cur_best != null) {
+ solution.addAll(cur_best);
+ }
+ solution.add(cur);
+ solutions[index] = solution;
+ findDP(solutions, items, index + 1);
+ }
+
+ private ArrayList setBest(ArrayList list1, ArrayList list2) {
+ if (list1 == null) {
+ return list2;
+ } else if (list2 == null) {
+ return list1;
+ }
+ if (list1.size() > list2.size()) {
+ return list1;
+ } else {
+ return list2;
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+
+ class HtWt {
+
+ public int height;
+ public int weight;
+
+ public HtWt() {
+ }
+
+ public HtWt(int h, int w) {
+ this.height = h;
+ this.weight = w;
+ }
+
+ public boolean canBeAfter(HtWt hw) {
+ return this.height > hw.height && this.weight > hw.height;
+ }
+
+ public boolean canBeBefore(HtWt hw) {
+ return this.height < hw.height && this.weight < hw.height;
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_8_RankNode.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_8_RankNode.java
new file mode 100644
index 0000000..71fb3a8
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/Q11_8_RankNode.java
@@ -0,0 +1,72 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 6, 2013
+ */
+package CareerCup.SortAndSearch;
+
+import javax.xml.crypto.Data;
+
+public class Q11_8_RankNode {
+
+ public class RankNode {
+
+ public int data = 0;
+ public int order = 0;
+ public RankNode left = null;
+ public RankNode right = null;
+
+ public RankNode(int _data, int _order) {
+ this.data = _data;
+ this.order = _order;
+ }
+
+ public void insert(int _data) {
+ if (_data < this.data) {
+ if (left == null) {
+ left = new RankNode(_data, 0);
+ } else {
+ left.insert(_data);
+ }
+ ++this.order;
+ } else if (_data > this.data) {
+ if (right == null) {
+ right = new RankNode(_data, 0);
+ } else {
+ right.insert(_data);
+ }
+ }
+ }
+
+ public int getOrder(int _data) {
+ if (_data == this.data) {
+ return this.order;
+ }
+ if (_data < this.data) {
+ if (left == null) {
+ return -1;
+ } else {
+ return left.getOrder(_data);
+ }
+ } else if (_data > this.data) {
+ if (right == null) {
+ return -1;
+ } else {
+ return 1 + this.order + right.getOrder(_data);
+ }
+ }
+ return -1;
+ }
+
+ public int getOrder() {
+ int order = this.order;
+ RankNode node = this;
+ while (node.right != null) {
+ order += right.order;
+ node = node.right;
+ }
+ return order;
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/QuickSort.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/QuickSort.java
new file mode 100644
index 0000000..2e757b3
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/QuickSort.java
@@ -0,0 +1,83 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.SortAndSearch;
+
+import Util.TestUtil;
+import java.util.Random;
+
+public class QuickSort> extends SortBase {
+
+ public QuickSort(T[] _array) {
+ super(_array);
+ }
+ public boolean Random = false;
+
+ @Override
+ void sort() {
+ split(0, array.length - 1);
+ }
+
+ void split(int s, int e) {
+ if (s >= e) {
+ return;
+ }
+ int pivot = 0;
+ if (Random) {
+ pivot = partitionRandom(s, e);
+ } else {
+ pivot = partition(s, e);
+ }
+ split(s, pivot - 1);
+ split(pivot + 1, e);
+ }
+
+ int partitionRandom(int s, int e) {
+ int pivot_ind = s + new Random().nextInt(e - s + 1);
+ T pivot_val = array[pivot_ind];
+ swap(pivot_ind, s);
+ int a = s + 1, b = s + 1;
+ while (b <= e) {
+ if (array[b].compareTo(pivot_val) < 0) {
+ swap(b, a++);
+ }
+ ++b;
+ }
+ swap(a - 1, s);
+ return a - 1;
+ }
+
+ int partition(int s, int e) {
+ T pivot = array[s];
+ while (s < e) {
+ for (; s < e && array[e].compareTo(pivot) >= 0; --e);
+ array[s] = array[e];
+ for (; s < e && array[s].compareTo(pivot) <= 0; ++s);
+ array[e] = array[s];
+ }
+ array[s] = pivot;
+ return s;
+ }
+
+ public static void main(String[] args) {
+ Integer[] arr = {8, 6, 4, 2, 3, 1, 2, 3, 4, 100, 9};
+ QuickSort qs = new QuickSort<>(arr);
+ qs.Random = true;
+ qs.sort();
+ for (Integer integer : qs.array) {
+ System.out.println(integer);
+ }
+
+ Integer[] test1 = TestUtil.intToInteger(TestUtil.generateArray(0xffff));
+ Integer[] test2 = test1.clone();
+ long t1 = System.currentTimeMillis();
+ QuickSort q1 = new QuickSort<>(test1);
+ q1.sort();
+ long t2 = System.currentTimeMillis();
+ QuickSort q2 = new QuickSort<>(test2);
+ q2.sort();
+ long t3 = System.currentTimeMillis();
+ long span1 = t2 - t1, span2 = t3 - t2;
+ System.out.print("Random:" + span1 + "\r\nFirst:" + span2);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/SelectSort.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/SelectSort.java
new file mode 100644
index 0000000..6e7e7ef
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/SelectSort.java
@@ -0,0 +1,36 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.SortAndSearch;
+
+public class SelectSort> extends SortBase {
+
+ public SelectSort(T[] _array) {
+ super(_array);
+ }
+
+ @Override
+ void sort() {
+ int k, i, j, length = array.length;
+ for (i = 0; i < length; i++) {
+ k = i;
+ for (j = i; j < length; j++) {
+ if (array[k].compareTo(array[j]) > 0) {
+ k = j;
+ }
+ }
+ if (k != i) {
+ swap(k, i);
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ Integer[] arr = {8, 6, 4, 2, 3};
+ SelectSort ss = new SelectSort<>(arr);
+ ss.sort();
+ for (Integer integer : ss.array) {
+ System.out.println(integer);
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/SortBase.java b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/SortBase.java
new file mode 100644
index 0000000..5df85a1
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/SortAndSearch/SortBase.java
@@ -0,0 +1,21 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 25, 2012
+ */
+package CareerCup.SortAndSearch;
+public abstract class SortBase {
+
+ public T[] array ;
+ public SortBase(T[] _array){
+ this.array = _array;
+ }
+
+ abstract void sort();
+
+ protected void swap(int a, int b)
+ {
+ T temp = array[a];
+ array[a] = array[b];
+ array[b] = temp;
+ }
+
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_1_ThreeStacks.java b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_1_ThreeStacks.java
new file mode 100644
index 0000000..595652d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_1_ThreeStacks.java
@@ -0,0 +1,166 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 3, 2013
+ */
+package CareerCup.StacksAndQueues;
+
+public class Q3_1_ThreeStacks {
+
+ final int NUM_BUFFER = 5;
+ final int NUM_STACK = 3;
+
+ public class Stack {
+
+ int[] buffer = new int[NUM_BUFFER];
+ StackData[] stack_data = new StackData[NUM_STACK];
+
+ public Stack() {
+ for (int i = 0; i < NUM_STACK; i++) {
+ stack_data[i] = new StackData(NUM_BUFFER / NUM_STACK * i, NUM_BUFFER / 3);
+ }
+ }
+
+ public int getTotalCapacity() {
+ int capacity = 0;
+ for (int i = 0; i < NUM_STACK; i++) {
+ capacity += stack_data[i]._capacity;
+ }
+ return capacity;
+ }
+
+ public boolean push(int val, int target) {
+ StackData sd = stack_data[target];
+ if (sd._pointer == sd._size - 1) {
+ expand(target);
+ } else if (getTotalCapacity() == NUM_BUFFER) {
+ System.out.println("Overflow");
+ return false;
+ }
+ buffer[next(sd._pointer)] = val;
+ sd._pointer = next(sd._pointer);
+ ++sd._size;
+ return true;
+ }
+
+ void expand(int target) {
+ StackData sd = stack_data[target];
+ shift((target + 1) % NUM_STACK);
+ ++sd._capacity;
+ }
+
+ void shift(int target) {
+ StackData sd = stack_data[target];
+ if (sd._capacity == sd._size) {
+ shift((target + 1) % NUM_STACK);
+ for (int i = (sd._start + sd._capacity - 1) % NUM_BUFFER; sd.isInStackData(i, NUM_BUFFER); i = last(i)) {
+ buffer[i] = buffer[last(i)];
+ }
+ sd._start = next(sd._start);
+ sd._pointer = next(sd._pointer);
+ } else {
+ for (int i = (sd._start + sd._capacity - 1) % NUM_BUFFER; sd.isInStackData(i, NUM_BUFFER); i = last(i)) {
+ buffer[i] = buffer[last(i)];
+ }
+ sd._start = next(sd._start);
+ sd._pointer = next(sd._pointer);
+ --sd._capacity;
+ }
+ }
+
+ public int pop(int target) {
+ StackData sd = stack_data[target];
+ if (sd._pointer < 0) {
+ System.out.println("No Data");
+ return Integer.MIN_VALUE;
+ }
+ int val = buffer[sd._pointer];
+ buffer[sd._pointer] = -1;
+ --sd._capacity;
+ sd._pointer = last(sd._pointer);
+ return val;
+ }
+
+ public int peek(int target) {
+ StackData sd = stack_data[target];
+ if (sd._pointer < 0) {
+ System.out.println("No Data");
+ return Integer.MIN_VALUE;
+ }
+ int val = buffer[sd._pointer];
+ return val;
+ }
+
+ int next(int i) {
+ if (i == NUM_BUFFER - 1) {
+ return 0;
+ } else {
+ return i + 1;
+ }
+ }
+
+ int last(int i) {
+ if (i == 0) {
+ return NUM_BUFFER - 1;
+ } else {
+ return i - 1;
+ }
+ }
+
+ void print() {
+ for (int i = 0; i < NUM_STACK; i++) {
+ StackData sd = stack_data[i];
+ System.out.print("T: " + i + " ");
+ for (int j = 0; j < sd._capacity; j++) {
+ System.out.print(buffer[sd._pointer + j]);
+ }
+ System.out.println();
+ }
+ }
+
+ void printbuffer() {
+ for (int i = 0; i < NUM_BUFFER; i++) {
+ System.out.print(buffer[i]);
+ }
+ }
+ }
+
+ class StackData {
+
+ int _pointer;
+ int _start;
+ int _capacity; //volumn
+ int _size; //used
+
+ public StackData(int start, int size) {
+ this._start = start;
+ this._size = size;
+ this._pointer = start - 1;
+ this._capacity = 0;
+ }
+
+ public boolean isInStackData(int i, int total) {
+ if (_start + _capacity < total) {
+ return i >= _start && i < _start + _capacity;
+ } else {
+ return (i >= _start && i < total) || (i < _start && i < (_size + _capacity) && i >= 0);
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ Q3_1_ThreeStacks q31 = new Q3_1_ThreeStacks();
+ Q3_1_ThreeStacks.Stack stack = q31.new Stack();
+ stack.push(9, 0);
+ stack.push(8, 0);
+ stack.push(7, 0);
+ stack.push(6, 0);
+ stack.push(5, 0);
+ System.out.println(stack.pop(0));
+ System.out.println(stack.pop(0));
+ System.out.println(stack.pop(0));
+ System.out.println(stack.pop(0));
+ stack.push(11, 1);
+ stack.push(22, 2);
+ System.out.println(stack.pop(2));
+ System.out.println(stack.pop(1));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_2_MinStack.java b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_2_MinStack.java
new file mode 100644
index 0000000..072b64c
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_2_MinStack.java
@@ -0,0 +1,55 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 3, 2013
+ */
+package CareerCup.StacksAndQueues;
+
+import java.util.Stack;
+
+public class Q3_2_MinStack {
+
+ class MinStack extends Stack {
+
+ Stack Helper;
+
+ public MinStack() {
+ super();
+ Helper = new Stack<>();
+ }
+
+ @Override
+ public Integer push(Integer e) {
+ if (Helper.isEmpty() || e < Helper.peek()) {
+ Helper.push(e);
+ }
+ return super.push(e);
+ }
+
+ @Override
+ public synchronized Integer pop() {
+ if (Helper.peek() == super.peek()) {
+ Helper.pop();
+ }
+ return super.pop();
+ }
+
+ public Integer min() {
+ if (Helper.isEmpty()) {
+ return Integer.MAX_VALUE;
+ }
+ return Helper.peek();
+ }
+ }
+
+ public static void main(String[] args) {
+ Q3_2_MinStack q32 = new Q3_2_MinStack();
+ Q3_2_MinStack.MinStack ms = q32.new MinStack();
+ ms.push(2);
+ ms.push(3);
+ ms.push(1);
+ ms.push(4);
+ System.out.println(ms.pop() + " Min:" + ms.min());
+ System.out.println(ms.pop() + " Min:" + ms.min());
+ System.out.println(ms.pop() + " Min:" + ms.min());
+ System.out.println(ms.peek() + " Min:" + ms.min());
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_3_SetOfStacks.java b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_3_SetOfStacks.java
new file mode 100644
index 0000000..89d1358
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_3_SetOfStacks.java
@@ -0,0 +1,105 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 3, 2013
+ */
+package CareerCup.StacksAndQueues;
+
+import java.util.ArrayList;
+import java.util.Stack;
+
+public class Q3_3_SetOfStacks {
+
+ final int NUM_LIMIT = 3;
+
+ public class SetOfStacks {
+
+ ArrayList> stacks;
+
+ public SetOfStacks() {
+ stacks = new ArrayList<>();
+ //stacks.add(new Stack());
+ }
+
+ public int popAt(int target) {
+ if (target == stacks.size()) {
+ System.out.println("No Data!!!");
+ return Integer.MIN_VALUE;
+ }
+ Stack stack = stacks.get(target);
+ int val = stack.pop();
+ shift(target);
+ return val;
+ }
+
+ private void shift(int target) {
+ if (target + 1 < stacks.size()) {
+ Stack stack = stacks.get(target);
+ Stack next_stack = stacks.get(target + 1);
+ stack.push(next_stack.firstElement());
+ next_stack.removeElementAt(next_stack.size() - 1);
+ shift(target + 1);
+ if (next_stack.isEmpty()) {
+ stacks.remove(target + 1);
+ }
+ }
+ }
+
+ private Stack getStack() {
+ if (stacks.isEmpty()) {
+ return null;
+ } else {
+ return stacks.get(stacks.size() - 1);
+ }
+ }
+
+ public void push(int e) {
+ Stack stack = getStack();
+ if (stack == null || stack.size() == NUM_LIMIT) {
+ stack = new Stack<>();
+ stacks.add(stack);
+ }
+ stack.add(e);
+ }
+
+ public int pop() {
+ Stack stack = getStack();
+ if (stack == null) {
+ System.out.println("No Data!!!");
+ return Integer.MIN_VALUE;
+ }
+ int val = stack.pop();
+ if (stack.isEmpty()) {
+ stacks.remove(stacks.size() - 1);
+ }
+ return val;
+ }
+
+ public int peek() {
+ Stack stack = getStack();
+ if (stack == null) {
+ System.out.println("No Data!!!");
+ return Integer.MIN_VALUE;
+ }
+ return stack.peek();
+ }
+
+ public void print() {
+ for (Stack stack : stacks) {
+ for (int i = 0; i < stack.size(); i++) {
+ System.out.print(stack.get(i));
+ }
+ System.out.println();
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ Q3_3_SetOfStacks q33 = new Q3_3_SetOfStacks();
+ Q3_3_SetOfStacks.SetOfStacks sos = q33.new SetOfStacks();
+ for (int i = 0; i < 10; i++) {
+ sos.push(i);
+ }
+ sos.print();
+ sos.popAt(1);
+ sos.print();
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_4_Hanoi.java b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_4_Hanoi.java
new file mode 100644
index 0000000..febc461
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_4_Hanoi.java
@@ -0,0 +1,62 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 3, 2013
+ */
+package CareerCup.StacksAndQueues;
+
+import LeetCode.OnlineJudge.Q34_StrStr;
+import java.util.Stack;
+
+public class Q3_4_Hanoi {
+
+ public class Hanoi {
+
+ Stack[] stack_list;
+
+ public Hanoi(int n) {
+ stack_list = new Stack[3];
+ for (int i = 0; i < 3; i++) {
+ stack_list[i] = new Stack<>();
+ }
+ for (int i = 1; i <= n; i++) {
+ stack_list[0].push(i);
+ }
+ }
+
+ public void solve() {
+ solve(stack_list[0], stack_list[1], stack_list[2], stack_list[0].size());
+ }
+
+ public void solve(Stack origin, Stack buffer, Stack destination, int n) {
+ if (n == 1) {
+ destination.push(origin.pop());
+ return;
+ }
+ solve(origin, destination, buffer, n - 1);
+ destination.push(origin.pop());
+ solve(buffer, origin, destination, n - 1);
+ }
+
+ private void solve(Stack origin, Stack buffer, Stack destination) {
+ buffer.push(origin.pop());
+ destination.push(origin.pop());
+ destination.push(buffer.pop());
+ }
+
+ public void print() {
+ for (Stack stack : stack_list) {
+ for (int i = 0; i < stack.size(); i++) {
+ System.out.print(stack.get(i));
+ }
+ System.out.println();
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ Q3_4_Hanoi q34 = new Q3_4_Hanoi();
+ Q3_4_Hanoi.Hanoi hanoi = q34.new Hanoi(5);
+ hanoi.print();
+ hanoi.solve();
+ hanoi.print();
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_5_QueueOn2Stack.java b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_5_QueueOn2Stack.java
new file mode 100644
index 0000000..c5363fe
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_5_QueueOn2Stack.java
@@ -0,0 +1,44 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.StacksAndQueues;
+
+import java.util.Stack;
+
+public class Q3_5_QueueOn2Stack {
+
+ public class Queue
+ {
+ Stack left,right;
+
+ public Queue()
+ {
+ left = new Stack<>();
+ right = new Stack<>();
+ }
+
+ public void enqueue(T val)
+ {
+ left.push(val);
+ }
+
+ public T dequeue()
+ {
+ if (right.isEmpty()) {
+ if (left.isEmpty()) {
+ System.out.println("No Data!!!");
+ return null;
+ }else{
+ while (!left.isEmpty()) {
+ right.push(left.pop());
+ }
+ }
+ }
+ return right.pop();
+ }
+ }
+
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_6_SortStack.java b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_6_SortStack.java
new file mode 100644
index 0000000..71d129d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_6_SortStack.java
@@ -0,0 +1,46 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.StacksAndQueues;
+
+import java.util.Stack;
+
+public class Q3_6_SortStack {
+
+ private Stack Helper = new Stack<>();
+
+ public void sort(Stack stack) {
+ while (true) {
+ while (Helper.isEmpty() || (!stack.isEmpty() && Helper.peek() <= stack.peek())) {
+ Helper.push(stack.pop());
+ }
+ if (stack.isEmpty()) {
+ break;
+ } else {
+ int val = stack.pop();
+ while (!Helper.isEmpty()) {
+ stack.push(Helper.pop());
+ }
+ stack.push(val);
+ }
+ }
+ while (!Helper.isEmpty()) {
+ stack.push(Helper.pop());
+ }
+ }
+
+ public static void main(String[] args) {
+ Stack stack = new Stack<>();
+ stack.push(2);
+ stack.push(5);
+ stack.push(4);
+ stack.push(3);
+ stack.push(6);
+ stack.push(1);
+ Q3_6_SortStack q36 = new Q3_6_SortStack();
+ q36.sort(stack);
+ for (int i = 0; i < stack.size(); i++) {
+ System.out.println(stack.get(i));
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_7_AnimalQueue.java b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_7_AnimalQueue.java
new file mode 100644
index 0000000..445de32
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/StacksAndQueues/Q3_7_AnimalQueue.java
@@ -0,0 +1,68 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.StacksAndQueues;
+
+import java.util.LinkedList;
+
+public class Q3_7_AnimalQueue {
+
+ public abstract class Animal {
+
+ public String name;
+ public int order;
+ }
+
+ public class Dog extends Animal {
+ }
+
+ public class Cat extends Animal {
+ }
+
+ public class AnimalQueue {
+
+ LinkedList dogs = new LinkedList<>();
+ LinkedList cats = new LinkedList<>();
+ private int order = 0;
+
+ public void enqueue(Animal animal) {
+ animal.order = this.order++;
+ if (animal instanceof Dog) {
+ dogs.add((Dog) animal);
+ } else if (animal instanceof Cat) {
+ cats.add((Cat) animal);
+ }
+ }
+
+ public Cat dequeueCat() {
+ return cats.poll();
+ }
+
+ public Dog dequeueDog() {
+ return dogs.poll();
+ }
+
+ public Animal dequeue() {
+ if (dogs.isEmpty()) {
+ return dequeueCat();
+ } else if (cats.isEmpty()) {
+ return dequeueDog();
+ }
+ if (dogs.peek().order < cats.peek().order) {
+ return dequeueDog();
+ } else {
+ return dequeueCat();
+ }
+ }
+ }
+
+ public static void main(String[] args) {
+ LinkedList ll = new LinkedList<>();
+ ll.add('a');
+ ll.add('b');
+ ll.add('c');
+ ll.add('d');
+ ll.add('e');
+ System.out.println(ll.pollLast());
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/App.java b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/App.java
new file mode 100644
index 0000000..e205d03
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/App.java
@@ -0,0 +1,19 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 26, 2012
+ */
+package CareerCup.ThreadsAndLocks.Q16_3_ChopStick;
+
+public class App {
+
+ public static void main(String[] args) {
+ Chopstick left = new Chopstick();
+ Chopstick right = new Chopstick();
+ Philosopher philosophers[] = new Philosopher[10];
+ for (int i = 0; i < 10; i++) {
+ philosophers[i] = new Philosopher(left, right, i + 1);
+ }
+ for (int i = 0; i < 10; i++) {
+ philosophers[i].start();
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/Chopstick.java b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/Chopstick.java
new file mode 100644
index 0000000..e8d4a0f
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/Chopstick.java
@@ -0,0 +1,27 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 26, 2012
+ */
+package CareerCup.ThreadsAndLocks.Q16_3_ChopStick;
+
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.locks.Lock;
+
+public class Chopstick {
+
+ Lock lock;
+
+ public Chopstick() {
+ lock = new ReentrantLock();
+ }
+
+ public boolean pickUp() {
+ return lock.tryLock();
+ }
+
+ public void putDown() {
+ lock.unlock();
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/Philosopher.java b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/Philosopher.java
new file mode 100644
index 0000000..28348ed
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_3_ChopStick/Philosopher.java
@@ -0,0 +1,71 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 26, 2012
+ */
+package CareerCup.ThreadsAndLocks.Q16_3_ChopStick;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class Philosopher extends Thread {
+
+ private Chopstick LeftStick;
+ private Chopstick RightStick;
+ int index;
+
+ public Philosopher(Chopstick _left, Chopstick _right, int index) {
+ this.LeftStick = _left;
+ this.RightStick = _right;
+ this.index = index;
+ }
+
+ public boolean pickUp() {
+ if (!LeftStick.pickUp()) {
+ return false;
+ }
+ if (!RightStick.pickUp()) {
+ LeftStick.putDown();
+ return false;
+ }
+ return true;
+ }
+
+ public void putDown() {
+ System.out.println(index + " put down!");
+ LeftStick.putDown();
+ RightStick.putDown();
+ }
+
+ public void chew() {
+ System.out.println(index + "Chew!");
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException ex) {
+ System.out.println("Sleep:" + ex.getMessage());
+ //Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ public void eat() {
+ if (pickUp()) {
+ chew();
+ putDown();
+ System.out.println(index + " success to pick up.");
+ } else {
+ try {
+ //System.out.println(index + " fail to pick up.");
+ Thread.sleep(1000);
+ eat();
+ } catch (InterruptedException ex) {
+ }
+
+ }
+ }
+
+ @Override
+ public void run() {
+ eat();
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/Foo.java b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/Foo.java
new file mode 100644
index 0000000..2fed292
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/Foo.java
@@ -0,0 +1,58 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ThreadsAndLocks.Q16_5_Foo;
+
+import java.util.concurrent.Semaphore;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class Foo {
+
+ public static Semaphore sp1, sp2, sp3;
+
+ public Foo() {
+ try {
+ sp1 = new Semaphore(1);
+ sp2 = new Semaphore(1);
+ sp3 = new Semaphore(1);
+
+ sp1.acquire();
+ sp2.acquire();
+ sp3.acquire();
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ //Logger.getLogger(Foo.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ public void first() {
+ System.out.println("first");
+ sp1.release();
+ }
+
+ public void second() {
+ try {
+ sp1.acquire();
+ sp1.release();
+ System.out.println("second");
+ sp2.release();
+ } catch (InterruptedException ex) {
+ Logger.getLogger(Foo.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ public void third() {
+ try {
+ sp2.acquire();
+ sp2.release();
+ System.out.println("third");
+ sp3.release();
+ } catch (InterruptedException ex) {
+ Logger.getLogger(Foo.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooLock.java b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooLock.java
new file mode 100644
index 0000000..3858a0b
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooLock.java
@@ -0,0 +1,60 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ThreadsAndLocks.Q16_5_Foo;
+
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class FooLock extends Thread {
+
+ public ReentrantLock lock1, lock2, lock3;
+
+ public FooLock() {
+ if (lock1 == null) {
+ lock1 = new ReentrantLock();
+ }
+ if (lock2 == null) {
+ lock2 = new ReentrantLock();
+ lock2.lock();
+ }
+ if (lock3 == null) {
+ lock3 = new ReentrantLock();
+ lock3.lock();
+ }
+ }
+
+ public boolean first() throws InterruptedException {
+ if (lock1.isLocked()) {
+ Thread.sleep(10000);
+ first();
+ }
+ System.out.println("first");
+ lock2.unlock();
+ return false;
+ }
+
+ public boolean second() throws InterruptedException {
+ if (lock2.isLocked()) {
+ Thread.sleep(10000);
+ second();
+
+ }
+ System.out.println("second");
+ lock3.unlock();
+ return false;
+ }
+
+ public boolean third() throws InterruptedException {
+ if (lock3.isLocked()) {
+ Thread.sleep(10000);
+ third();
+ }
+ System.out.println("third");
+ return false;
+ }
+
+ public static void main(String[] args) throws InterruptedException {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooLockThread.java b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooLockThread.java
new file mode 100644
index 0000000..2fb87f1
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooLockThread.java
@@ -0,0 +1,46 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ThreadsAndLocks.Q16_5_Foo;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class FooLockThread extends Thread {
+
+ int i;
+ FooLock fl;
+
+ public FooLockThread(int i) {
+ this.i = i;
+ fl = new FooLock();
+ }
+
+ @Override
+ public void run() {
+ try {
+ switch (i) {
+ case 1:
+
+ fl.first();
+
+ break;
+ case 2:
+ fl.second();
+ break;
+ case 3:
+ fl.third();
+ break;
+ }
+ } catch (InterruptedException ex) {
+ Logger.getLogger(FooLockThread.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ public static void main(String[] args) {
+ for (int i = 0; i < 3; i++) {
+ FooLockThread flt = new FooLockThread(i + 1);
+ flt.start();
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooThread.java b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooThread.java
new file mode 100644
index 0000000..d3341b2
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/ThreadsAndLocks/Q16_5_Foo/FooThread.java
@@ -0,0 +1,37 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Dec 27, 2012
+ */
+package CareerCup.ThreadsAndLocks.Q16_5_Foo;
+
+public class FooThread extends Thread {
+
+ Foo f;
+ int i;
+
+ public FooThread(int i) {
+ f = new Foo();
+ this.i = i;
+ }
+
+ @Override
+ public void run() {
+ switch (i) {
+ case 1:
+ f.first();
+ break;
+ case 2:
+ f.second();
+ break;
+ case 3:
+ f.third();
+ break;
+ }
+ }
+
+ public static void main(String[] args) {
+ for (int i = 0; i < 3; i++) {
+ FooThread th = new FooThread(i + 1);
+ th.start();
+ }
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/CompleteTree.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/CompleteTree.java
new file mode 100644
index 0000000..2067db9
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/CompleteTree.java
@@ -0,0 +1,47 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Mar 17, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import Util.BinaryTreeNode;
+
+public class CompleteTree {
+
+ //level init as 0
+ public boolean verify(BinaryTreeNode root, int level) {
+ if (root == null) {
+ return true;
+ }
+ boolean l = verify(root.Left, level + 1);
+ if (!l) {
+ return false;
+ }
+ boolean r = verify(root.Right, level + 1);
+ if (!r) {
+ return false;
+ }
+ if (root.Left == null || root.Right == null) {
+ if (MaxLevel == -1) {
+ MaxLevel = level;
+ } else {
+ if (MaxLevel == level && !LoopFlag) {
+ return true;
+ } else if (MaxLevel - 1 == level) {
+ LoopFlag = true;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ }
+ return l && r;
+ }
+ static int MaxLevel = -1;
+ static boolean LoopFlag = false;
+
+ public static void main(String[] args) {
+ BinaryTreeNode root = BinaryTreeNode.getSampleTree5();
+ boolean result = new CompleteTree().verify(root, 0);
+ System.out.println(result);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q1_3_BuildTree.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q1_3_BuildTree.java
new file mode 100644
index 0000000..222350d
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q1_3_BuildTree.java
@@ -0,0 +1,23 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import Util.BinaryTreeNode;
+
+public class Q1_3_BuildTree {
+
+ public BinaryTreeNode build(int[] arr, int s, int e) {
+ if (s > e) {
+ return null;
+ }
+ int m = s + (e - s) / 2;
+ BinaryTreeNode root = new BinaryTreeNode<>(arr[m]);
+ root.Left = build(arr, s, m - 1);
+ root.Right = build(arr, m + 1, e);
+ return root;
+ }
+
+ public static void main(String[] args) {
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_1_IsBalanceTree.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_1_IsBalanceTree.java
new file mode 100644
index 0000000..c65188a
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_1_IsBalanceTree.java
@@ -0,0 +1,40 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import LeetCode.OnlineJudge.Q41_LengthOfLastWord;
+import Util.BinaryTreeNode;
+
+public class Q4_1_IsBalanceTree {
+
+ public boolean isBalance(BinaryTreeNode root) {
+ int dif = isBalanceHelper(root);
+ return dif != -1;
+ }
+
+ public int isBalanceHelper(BinaryTreeNode root) {
+ if (root == null) {
+ return 0;
+ }
+ int l = isBalanceHelper(root.Left);
+ if (l == -1) {
+ return -1;
+ }
+ int r = isBalanceHelper(root.Right);
+ if (r == -1) {
+ return -1;
+ }
+ int dif = Math.abs(l - r);
+ if (dif > 2) {
+ return -1;
+ }
+ return dif + 1;
+ }
+
+ public static void main(String[] args) {
+ BinaryTreeNode root = BinaryTreeNode.getSampleTree();
+ Q4_1_IsBalanceTree q41 = new Q4_1_IsBalanceTree();
+ System.out.println(q41.isBalanceHelper(root));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_2_BFSOnGraph.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_2_BFSOnGraph.java
new file mode 100644
index 0000000..7f6cc89
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_2_BFSOnGraph.java
@@ -0,0 +1,42 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import LeetCode.OnlineJudge.Q42_LetterCombinationPhoneNum;
+import Util.GraphNode;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class Q4_2_BFSOnGraph {
+
+ public boolean isConnect(GraphNode root) {
+ Queue> q = new LinkedList<>();
+ q.add(root);
+ while (!q.isEmpty()) {
+ GraphNode cur = q.poll();
+ cur.state = 1;
+ ArrayList> nodes = cur.Nodes;
+ for (GraphNode node : nodes) {
+ if (node.state == 1) {
+ System.out.println(node.Data);
+ return false;
+ } else if (node.state == 0) {
+ q.add(node);
+ } else {
+ System.out.println("ERROR");
+ }
+ }
+ }
+ return true;
+ }
+
+ public static void main(String[] args) {
+ GraphNode root1 = GraphNode.getSampleGraphNode();
+ GraphNode root2 = GraphNode.getSampleGraphNode2();
+ Q4_2_BFSOnGraph q42 = new Q4_2_BFSOnGraph();
+ q42.isConnect(root1);
+ q42.isConnect(root2);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_4_CreateLevelList.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_4_CreateLevelList.java
new file mode 100644
index 0000000..8e49873
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_4_CreateLevelList.java
@@ -0,0 +1,76 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import Util.BinaryTreeNode;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class Q4_4_CreateLevelList {
+
+ public ArrayList>> getLevelList2(BinaryTreeNode root) {
+ ArrayList>> solution = new ArrayList<>();
+ getLevelList2Helper(solution, 0, root);
+ return solution;
+ }
+
+ private void getLevelList2Helper(ArrayList>> solution, int level, BinaryTreeNode root) {
+ LinkedList> list = null;
+ if (level >= solution.size()) {
+ list = new LinkedList<>();
+ solution.add(list);
+ } else {
+ list = solution.get(level);
+ }
+ list.add(root);
+ if (root.Left != null) {
+ getLevelList2Helper(solution, 1 + level, root.Left);
+ }
+ if (root.Right != null) {
+ getLevelList2Helper(solution, 1 + level, root.Right);
+ }
+ }
+
+ public ArrayList>> getLevelList(BinaryTreeNode root) {
+ ArrayList>> solution = new ArrayList<>();
+ LinkedList> list = new LinkedList<>();
+ if (root != null) {
+ list.add(root);
+ solution.add(list);
+ }
+ while (!list.isEmpty()) {
+ LinkedList> next_list = new LinkedList<>();
+ for (int i = 0; i < list.size(); i++) {
+ BinaryTreeNode cur = list.get(i);
+ if (cur.Left != null) {
+ next_list.add(cur.Left);
+ }
+ if (cur.Right != null) {
+ next_list.add(cur.Right);
+ }
+ }
+ list = next_list;
+ solution.add(list);
+ }
+ return solution;
+ }
+
+ public static void main(String[] args) {
+ Q4_4_CreateLevelList q44 = new Q4_4_CreateLevelList();
+ BinaryTreeNode root = BinaryTreeNode.getSampleTree();
+ ArrayList>> solution = q44.getLevelList2(root);
+ print(solution);
+ }
+
+ private static void print(ArrayList>> solution) {
+ for (LinkedList> list : solution) {
+ for (BinaryTreeNode binaryTreeNode : list) {
+ System.out.print(binaryTreeNode.Data + " ");
+ }
+ System.out.println();;
+ }
+
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_5_ValidBinarySearchTree.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_5_ValidBinarySearchTree.java
new file mode 100644
index 0000000..0a69c46
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_5_ValidBinarySearchTree.java
@@ -0,0 +1,47 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import Util.BinaryTreeNode;
+
+public class Q4_5_ValidBinarySearchTree {
+
+ public boolean isSearchTreeS(BinaryTreeNode root, int min, int max) {
+ if (root == null) {
+ return true;
+ }
+ if (root.Data > min || root.Data < max) {
+ return false;
+ }
+ boolean result = true;
+ result &= isSearchTreeS(root.Right, root.Data, max) && isSearchTreeS(root.Left, min, root.Data);
+ return result;
+ }
+
+ public boolean isSearchTree(BinaryTreeNode root, int min, int max) {
+ if (root == null) {
+ return true;
+ }
+ if (root.Left != null) {
+ if (min > root.Left.Data || root.Data < root.Left.Data) {
+ return false;
+ }
+ } else if (root.Right != null) {
+ if (max < root.Right.Data || root.Data > root.Right.Data) {
+ return false;
+ }
+ }
+ boolean result = true;
+ result &= isSearchTree(root.Right, root.Data, max) && isSearchTree(root.Left, min, root.Data);
+ return result;
+ }
+
+ public static void main(String[] args) {
+ BinaryTreeNode bstree = BinaryTreeNode.getSampleTree();
+ BinaryTreeNode ubstree = BinaryTreeNode.getSampleTree2();
+ Q4_5_ValidBinarySearchTree q45 = new Q4_5_ValidBinarySearchTree();
+ System.out.println(q45.isSearchTree(bstree, Integer.MIN_VALUE, Integer.MAX_VALUE));
+ System.out.println(q45.isSearchTree(ubstree, Integer.MIN_VALUE, Integer.MAX_VALUE));
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_6_NextNode.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_6_NextNode.java
new file mode 100644
index 0000000..340ddbd
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_6_NextNode.java
@@ -0,0 +1,33 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import Util.BinaryTreeNode;
+
+public class Q4_6_NextNode {
+
+ public BinaryTreeNode next(BinaryTreeNode node) {
+ BinaryTreeNode runner = node;
+ if (runner.Right != null) {
+ runner = runner.Right;
+ while (runner.Left != null) {
+ runner = runner.Left;
+ }
+ return runner;
+ }else{
+ BinaryTreeNode parent = runner.Parent;
+ while (parent.Left == runner) {
+ runner = parent;
+ parent = runner.Parent;
+ }
+ return parent;
+ }
+ }
+
+ public static void main(String[] args) {
+ BinaryTreeNode node = BinaryTreeNode.getSampleTree();
+ Q4_6_NextNode q46 = new Q4_6_NextNode();
+ System.out.println(q46.next(node).Data);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_7_CommonAncester.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_7_CommonAncester.java
new file mode 100644
index 0000000..0ad0740
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_7_CommonAncester.java
@@ -0,0 +1,75 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 4, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import Util.BinaryTreeNode;
+
+public class Q4_7_CommonAncester {
+
+ public BinaryTreeNode find(BinaryTreeNode root, BinaryTreeNode p, BinaryTreeNode q) {
+ Wrapper wr = findHelper(root, p, q);
+ if (wr.find) {
+ return wr.node;
+ } else {
+ return null;
+ }
+ }
+
+ public Wrapper findHelper(BinaryTreeNode root, BinaryTreeNode p, BinaryTreeNode q) {
+ Wrapper wr = new Wrapper();
+ if (root == null) {
+ if (p == root) {
+ wr.node = p;
+ } else if (q == root) {
+ wr.node = q;
+ }
+ return wr;
+ }
+ Wrapper l = findHelper(root.Left, p, q);
+ if (l.find) {
+ return l;
+ }
+ Wrapper r = findHelper(root.Right, p, q);
+ if (r.find) {
+ return r;
+ }
+ if (l.node != null && r.node != null) {
+ wr.node = root;
+ wr.find = true;
+ return wr;
+ } else if (root == p || root == q) {
+ wr.find = l.node != null || r.node != null;
+ wr.node = root;
+ } else {
+ wr.find = false;
+ if (p == root) {
+ wr.node = p;
+ } else if (q == root) {
+ wr.node = q;
+ }
+ }
+ return wr;
+ }
+
+ class Wrapper {
+
+ BinaryTreeNode node;
+ boolean find;
+
+ public Wrapper() {
+ }
+
+ public Wrapper(BinaryTreeNode _node, boolean _find) {
+ this.node = _node;
+ this.find = _find;
+ }
+ }
+
+ public static void main(String[] args) {
+ BinaryTreeNode root = BinaryTreeNode.getSampleTree();
+ Q4_7_CommonAncester q47 = new Q4_7_CommonAncester();
+ BinaryTreeNode ancester = q47.find(root, root.Left.Left, root.Left);
+ System.out.println(ancester.Data);
+ }
+}
diff --git a/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_8_SubTree.java b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_8_SubTree.java
new file mode 100644
index 0000000..884a4f4
--- /dev/null
+++ b/Algorithm-Java-Backup/src/CareerCup/TreesAndGraphs/Q4_8_SubTree.java
@@ -0,0 +1,65 @@
+/**
+ * Sanqiang Zhao Www.131X.Com Jan 5, 2013
+ */
+package CareerCup.TreesAndGraphs;
+
+import Util.BinaryTreeNode;
+
+public class Q4_8_SubTree {
+
+ public boolean isSubtree(BinaryTreeNode large, BinaryTreeNode