Permalink
Browse files

Final version

  • Loading branch information...
1 parent 5b6b8f0 commit ae06d5375cf9219a708c15cb48ab840cb68ce470 @willawill committed Jan 28, 2012
View
BIN BinaryTree/BST.class
Binary file not shown.
View
401 BinaryTree/BST.java
@@ -0,0 +1,401 @@
+public class BST {
+
+ public Node root = null; // Root node of the tree
+ Node currentNode = root;
+
+ // Checks to see if is empty. Prints out message if it is.
+ public boolean IsEmpty() {
+ if (root == null)
+ return true;
+ return false;
+ }// End of IsEmpty
+
+ Node create(int number) {
+ Node node = new Node();
+ node.number = number;
+ node.parent = null;
+ node.leftChild = null;
+ node.rightChild = null;
+ return node;
+ }
+
+ public int size() {
+ return size(root);
+ }
+
+ public int size(Node node) {
+ if (node == null) return 0;
+ else {
+ return size(node.leftChild) + 1 + size(node.rightChild);
+ }
+ }
+
+ // Method to insert a number into the tree.
+ public void insert(int number) {
+
+ currentNode = add(root, number);
+ if (IsEmpty() == true && currentNode != null)
+ root = currentNode;
+ // currentNode = root;
+ }
+
+ Node add(Node node, int number) {
+ if (node == null)// create a new node.
+ {
+ node = create(number);
+ return node;
+
+ } else if (number < node.number)// if the number is less than the node,
+ // go to the left child.
+ {
+ if (node.leftChild != null)
+ add(node.leftChild, number);
+ else
+ node.leftChild = create(number);
+
+ } else if (number >= node.number)// if the number is greater than the
+ // node, go to the right child
+ {
+ if (node.rightChild != null)
+ add(node.rightChild, number);
+ else
+ node.rightChild = create(number);
+
+ }
+
+ return null;
+
+ }// end BSTNode insert
+
+ public void find(int number)
+ {
+ IsEmpty();
+
+ currentNode = find(root, number);
+
+ System.out.println("Search returned Node: " + currentNode.number);
+ System.out.println("Node's left child: " + currentNode.leftChild);
+ System.out.println("Node's right child: " + currentNode.rightChild);
+
+ }//End of find
+
+ Node find(Node currentNode, int number) {
+ // currentNode = root; //sets the root node to the current node for
+ // searching purposes
+
+ while (currentNode.number != number && currentNode != null) {
+ if (number < currentNode.number) // if number we are searching for
+ // is less than the node <strong
+ // class="highlight">search</strong>
+ // left sub <strong
+ // class="highlight">tree</strong>
+ {
+ currentNode = currentNode.leftChild;
+ } else if (number > currentNode.number)// if number we are searching
+ // for is greater than the
+ // node <strong
+ // class="highlight">search</strong>
+ // right sub <strong
+ // class="highlight">tree</strong>
+ {
+ currentNode = currentNode.rightChild;
+ } else // if number we are searching for is equal to the node
+ // <strong class="highlight">search</strong> right sub
+ // <strong class="highlight">tree</strong>
+ {
+ currentNode = currentNode.rightChild;
+ }
+ }
+
+ return currentNode;
+
+ }// End of RBTNode find
+
+ public Node getRoot() {
+ return root;
+ }
+
+ public void display() {
+ display(root);
+ }
+
+ private void display(Node input) {
+
+ if (input == null) {
+ return;
+ }
+
+ display(input.leftChild);
+ System.out.println(input.number);
+ display(input.rightChild);
+
+ }// End of display
+
+ public static boolean isBST(Node x){
+ if(x == null) return false;
+ else if(x.leftChild == null && x.rightChild == null) return true;
+ else if(x.leftChild != null && x.rightChild == null){
+ if(x.leftChild.number > x.number) return false;
+ else return isBST(x.leftChild);
+ } else if(x.rightChild != null && x.leftChild == null){
+ if(x.rightChild.number < x.number) return false;
+ else return isBST(x.rightChild);
+ } else {
+ if(x.leftChild.number > x.number || x.rightChild.number < x.number) return false;
+ else return isBST(x.leftChild) && isBST(x.rightChild);
+ }
+ }
+
+ //Deletion
+ public void delete(int value) {
+ root = delete(root, value);
+ }
+
+ private Node delete(Node x, int value) {
+ if(x == null) return null;
+ if(value < x.number)
+ {
+ x.leftChild = delete(x.leftChild, value);
+ }
+ else if(value > x.number)
+ {
+ x.rightChild = delete(x.rightChild, value);
+ }
+ else if(x.leftChild != null && x.rightChild != null){
+ x.number = minValue(x.rightChild);
+ x.rightChild = removeMin(x.rightChild);
+ }
+ else{
+ x = (x.leftChild != null) ? x.leftChild: x.rightChild;
+ }
+ return x;
+ }
+
+ private Node removeMin(Node x) {
+ if(x == null) return null;
+ else if(x.leftChild != null) {
+ x.leftChild = removeMin(x.leftChild);
+ return x;
+ } else {
+ return x.rightChild;
+ }
+ }
+
+ //Max Depth
+ public int maxDepth(){
+ return maxDepth(root);
+ }
+
+ private int maxDepth(Node x){
+ int lDepth, rDepth;
+ if(x == null) return 0;
+
+ lDepth = maxDepth(x.leftChild);
+ rDepth = maxDepth(x.rightChild);
+ return (lDepth > rDepth ? lDepth : rDepth) + 1;
+ }
+
+ //Min Value
+ public int minValue() {
+ return minValue(root);
+ }
+
+ private int minValue(Node node) {
+ if(node == null) return 0;
+ Node current = node;
+ while(current.leftChild != null) {
+ current = current.leftChild;
+ }
+
+ return current.number;
+ }
+
+ //Order walk
+
+ public void inorderWalk() {
+ inorderWalk(root);
+ }
+
+ private void inorderWalk(Node x) {
+ if( x != null){
+ inorderWalk(x.leftChild);
+ System.out.println(x.number);
+ inorderWalk(x.rightChild);
+ }
+ }
+
+ public void preorderWalk() {
+ preorderWalk(root);
+ }
+
+ private void preorderWalk(Node x) {
+ if(x != null){
+ System.out.println(x.number);
+ preorderWalk(x.leftChild);
+ preorderWalk(x.rightChild);
+ }
+ }
+
+ public void postorderWalk() {
+ postorderWalk(root);
+ }
+
+ private void postorderWalk(Node x) {
+ if(x != null) {
+ postorderWalk(x.leftChild);
+ postorderWalk(x.rightChild);
+ System.out.println(x.number);
+ }
+ }
+
+ //has Path Sum
+ public boolean hasPathSum(int sum){
+ return hasPathSum(root, sum);
+ }
+
+ private boolean hasPathSum(Node node, int sum) {
+ if(node == null) return sum == 0;
+ else {
+ int subSum = sum - node.number;
+ return hasPathSum(node.leftChild, subSum) || hasPathSum(node.rightChild, subSum);
+ }
+ }
+
+ //Print Path
+ public void printPaths(){
+ int[] path = new int[1000];
+ printPaths(root, path, 0);
+ }
+
+ private void printPaths(Node node, int[] path, int pathLen) {
+ if(node == null) return;
+ path[pathLen] = node.number;
+ pathLen++;
+
+ if(node.leftChild == null && node.rightChild == null)
+ printArray(path, pathLen);
+ else {
+ printPaths(node.leftChild, path, pathLen);
+ printPaths(node.rightChild, path, pathLen);
+ }
+ }
+
+ private void printArray(int[] ints, int len) {
+ for(int i = 0; i<len; i++) {
+ System.out.print(ints[i] + " ");
+ }
+ System.out.println();
+ }
+
+ public void mirror() {
+ mirror(root);
+ }
+
+ private void mirror(Node node) {
+ if(node != null){
+ mirror(node.leftChild);
+ mirror(node.rightChild);
+
+ Node temp = node.leftChild;
+ node.leftChild = node.rightChild;
+ node.rightChild = temp;
+ }
+ }
+
+ public void doubleTree() {
+ doubleTree(root);
+ }
+
+ private void doubleTree(Node node) {
+ Node oldLeft;
+ if (node == null) return;
+
+ doubleTree(node.leftChild);
+ doubleTree(node.rightChild);
+
+ oldLeft = node.leftChild;
+ node.leftChild = new Node(node.number);
+ node.leftChild.leftChild = oldLeft;
+ }
+
+ public static void main(String[] args)
+ {
+ BST BST = new BST();
+
+ int inputArray[] = new int[10];
+// inputArray[0] = 26;
+// for(int i = 1; i < 10; i++){
+// inputArray[i] = i*i;
+// }
+// for(int i = 0; i < 10; i++){
+// RBT.insert(inputArray[i]);
+// }
+ BST.insert(5);
+ BST.insert(3);
+ BST.insert(4);
+ BST.insert(8);
+ BST.insert(9);
+ BST.insert(1);
+ BST.insert(12);
+ System.out.println(BST.hasPathSum(5));
+
+ //BST.delete(111);
+ // System.out.println("===IN ORDER WALK===");
+ // BST.inorderWalk();
+ // System.out.println("===PRE ORDER WALK===");
+ // BST.preorderWalk();
+ // System.out.println("===POST ORDER WALK===");
+ // BST.postorderWalk();
+
+ //BST.display(BST.getRoot());
+ System.out.println();
+
+
+ /*
+ Scanner console = new Scanner(System.in);
+
+ System.out.println("How many numbers would you like to enter into the Red Black Tree?");
+ int k = console.nextInt();
+
+ int inputarray[] = new int[k];
+
+ for(int i=0; i< k; i++)
+ {
+
+ System.out.println("Please enter the " + (i+1)+ " number: ");
+ inputarray[i]= console.nextInt();
+ }
+
+ System.out.println("\n\n****Display Start****");
+ for(int i=0; i<k; i++)
+ {
+ RBT.insert(inputarray[i]);
+ }
+ System.out.println(isBST(RBT.root));
+ RBT.display(RBT.getRoot());
+ System.out.println(RBT.root);
+ */
+ }
+
+}// end of class RBTTree
+
+class Node
+{
+ int number;
+ Node parent;
+ Node leftChild;
+ Node rightChild;
+
+ Node(){
+ number = 0;
+ leftChild = null;
+ rightChild = null;
+ }
+
+ Node(int data){
+ number = data;
+ leftChild = null;
+ rightChild = null;
+ }
+
+}
View
BIN BinaryTree/BSTNode.class
Binary file not shown.
View
BIN BinaryTree/BinarySearchTree.class
Binary file not shown.
View
21 BinaryTree/BinarySearchTree.java
@@ -282,6 +282,7 @@ private int getHeight(BSTNode node){
}
}
+
public boolean isBalanced2(BSTNode node){
if (getHeight(node) == -1){
return false;
@@ -293,25 +294,9 @@ public boolean isBalanced2(BSTNode node){
public BSTNode getRoot(){
return root;
}
-
- public static void main(String[] args){
- BinarySearchTree bst = new BinarySearchTree();
- bst.insert(5);
- bst.insert(6);
- bst.insert(2);
- bst.insert(8);
- bst.insert(1);
- bst.insert(3);
- bst.inorderWalk2();
- BSTNode root = bst.getRoot();
- System.out.println(bst.Depth());
- System.out.println("The bst is balanced? " + bst.isBalanced2(root));
- // System.out.println(bst.getSize());
- System.out.println();
- System.out.println(bst.isValid(root));
+ public void setRoot(BSTNode n){
+ root = n;
}
-
-
}
class Stack{
BSTNode[] array;
View
BIN BinaryTree/MinimalHeightTree.class
Binary file not shown.
View
BIN BinaryTree/Node.class
Binary file not shown.
View
BIN BinaryTree/Stack.class
Binary file not shown.
View
27 Div.java
@@ -0,0 +1,27 @@
+public class Div{
+
+ public static int div(int x, int y){
+ if (y == 0) return -1;
+ int ans = 0;
+ for (int i = 31; i >= 0;i--){
+ if ((x>>i) != 0){
+ System.out.println((x>>i) + "!!!"+ i);}
+
+ if ((x>>i)>=y){
+
+ ans += (1<<i);
+ System.out.println(ans);
+ x -= (y<<i);
+ System.out.println("The" + (y<<i));
+ }
+
+ }
+ return ans;
+ }
+
+ public static void main(String[] args){
+ //System.out.println(Div.div(15, 3));
+ System.out.println(5<<2);
+ System.out.println(Integer.toString(3>>1,2));
+ }
+}
View
29 FibonacciNumber.java
@@ -0,0 +1,29 @@
+public class FibonacciNumber{
+ private static final int max = 1000;
+ private long[] fib;
+
+ public FibonacciNumber(){
+ fib = new long[max];
+ }
+
+ public long fib(long i){
+ if (i == 0) return 0;
+ if (i == 1) return 1;
+ if (fib[i] != 0) return fib[i];
+ fib[i] = fib(i-1) + fib(i-2);
+ System.out.println(fib[i]);
+ return fib[i];
+ }
+ public long fib2(long i){
+ if (i == 0 ) return 0;
+ if (i == 1 ) return 1;
+ return fib2(i-1)+fib2(i-2);
+ }
+
+ public static void main(String[] args){
+ FibonacciNumber fn = new FibonacciNumber();
+ System.out.println(fn.fib(1000));
+
+
+ }
+}
View
BIN Hacking_a_Google_Interview_Handout_1.pdf
Binary file not shown.
View
BIN Hacking_a_Google_Interview_Handout_2.pdf
Binary file not shown.
View
BIN Hacking_a_Google_Interview_Practice_Questions_Person_A.pdf
Binary file not shown.
View
BIN Hacking_a_Google_Interview_Practice_Questions_Person_B.pdf
Binary file not shown.
View
BIN LinkedList/LinkedList$IntWrapper.class
Binary file not shown.
View
BIN LinkedList/LinkedList.class
Binary file not shown.
View
25 LinkedList/LinkedList.java
@@ -3,12 +3,23 @@
public class LinkedList{
private LinkedListNode head;
private int size;
-
public LinkedList(){
this.head = null;
this.size = 0;
}
+ public void reverse(){
+ LinkedListNode current, next, loop;
+ current = head; next = current.getNext(); loop = null;
+ while (next != null){
+ current.setNext(loop);
+ loop = current;
+ current = next;
+ next = next.getNext();
+ }
+ head = current;
+ head.setNext(loop);
+ }
public void add(Object s){
LinkedListNode temp = new LinkedListNode(s);
if (this.size == 0){
@@ -122,12 +133,7 @@ public static void main(String[] args){
l.insert(6,2);
System.out.println(l.remove(3));
- //l.removeDuplicates();
- l.removeDupNoHashset();
- l.deleteMidEle(4);
-
- System.out.println(l.returnKth3(3).getValue());
- System.out.println(l.get(2));
+ l.reverse();
System.out.println(l.toString());
}
@@ -147,7 +153,6 @@ public void removeDupNoHashset(){
previous.setNext(temp);
current = temp;
break;
-
}
count ++;
runner = runner.getNext();
@@ -158,12 +163,10 @@ public void removeDupNoHashset(){
previous = current;
current = current.getNext();
count ++;
-
}
-
}
}
-
+
public LinkedListNode returnKth(int k){
if ((k < 0)||(k > this.size)){
return null;
View
BIN LinkedList/LinkedListNode.class
Binary file not shown.
View
BIN LinkedList/atoi.class
Binary file not shown.
View
39 LinkedList/atoi.java
@@ -0,0 +1,39 @@
+public class atoi{
+
+ public static int atoi(String s){
+ if (s == null|| s.trim().length() == 0){
+ throw new NullPointerException("!!");
+ }
+ char[] s1 = s.trim().toCharArray();
+ int start = 0;
+ int result = 0;
+ boolean isNeg = false;
+ if (s1[start] == '-' || s1[start] == '+'){
+ if (s1[start] == '-'){
+ isNeg = true;
+ start ++;
+ }
+ }
+ int len = s1.length;
+ for (;start<len;start ++){
+ if (s1[start] < '0'||s1[start] > '9'){
+ throw new IllegalArgumentException("!!!");
+ }
+ else{
+ char digit = (char)(s1[start] - '0');
+ result += (digit* Math.pow(10, (len-start-1)));
+ }
+ }
+ if (isNeg){
+ return 0-result;
+ }
+ else
+ return result;
+
+ }
+
+ public static void main(String[] args){
+ String s = "12345";
+ System.out.println(atoi.atoi(s));
+ }
+}
View
63 LinkedStack.java
@@ -0,0 +1,63 @@
+public class linkedStack(){
+
+
+private LinkedNode top;
+private int count;
+
+public linkedStack(){
+ count = 0;
+ top = null;
+}
+
+public void push(T element){
+ LinedNode n = new LinkedNode(element);
+ top.setNext(n);
+ top = n;
+ count ++;
+}
+
+public T pop(){
+ if (top == null){
+ throw new EmptyCollectionException(“Stack”);
+
+}
+ T element = top.getElement();
+ top = top.getNext();// top = top.next. Because first points to the next.
+ count --;
+ return element;
+
+}
+
+}
+
+class LinkedNode<T>(){
+
+private LinkedNode<T> next;
+private T element;
+
+public LinkedNode(){
+ next = null;
+ element = null;
+
+}
+
+public LinkedNode(T element){
+ next = null;
+ this.element = element;
+
+}
+public LinkedNode<T> getNext(){
+ return next;
+}
+public void setNext(LinkedNode<T> n){
+ this.next = n;
+}
+
+public T getElement(){
+ return element;
+}
+
+public void setElement(T element){
+ this.element = element;
+}
+}
View
4 MergeSort.java
@@ -12,7 +12,7 @@ public MergeSort( int size )
// fill array with random ints in range 10-99
for ( int i = 0; i < size; i++ )
- data[ i ] = 10 + generator.nextInt( 90 );
+ data[ i ] = 10 + gxenerator.nextInt( 90 );
} // end MergeSort constructor
// calls recursive split method to begin merge sorting
@@ -27,7 +27,7 @@ private void sortArray( int low, int high )
// test base case; size of array equals 1
if ( ( high - low ) >= 1 ) // if not base case
{
- int middle1 = ( low + high ) / 2; // calculate middle of array
+ int middle1 = ( low + high ) >> 1; // calculate middle of array
int middle2 = middle1 + 1; // calculate next element over
// output split step
View
BIN Parking Design Standards w-comment.pdf
Binary file not shown.
View
2 Permutations.java
@@ -34,7 +34,7 @@ private void perm1(String prefix, String s) {
}
public static void main(String[] args) {
Permutations p = new Permutations();
- String alphabet = "Te";
+ String alphabet = "Tee";
p.perm1(alphabet);
}
View
142 RestaurantReservation/Restaurant.java
@@ -0,0 +1,142 @@
+/*Need to figure out it's fit for one restaurant or it's a system with many restaurant--Only for one.
+ Need to figure out the range of future reservation.--Next two weeks.
+
+ Need to clarify buffet or entees?
+ Suppose one group table =
+*/
+public class Restaurant{
+ Rinfo rinfo;
+ TableManager tm;
+
+}
+
+class TableManager{
+ HashMap<String, Integer> resWithName;//Used to locate customers.
+ HashMap<Date, HashMap<String, ArrayList>> resWithDate;//Used to check availiability.
+ // e.g. Jan.5 -> <11:00 ->Table1>
+ // <11:30 ->Table1>
+ int Table tables;
+ ArrayList<Request> waitingList;//Can be designed as a command pattern.When a cancelation request come, check if there is available spot for customer.
+
+ int numOfTable;
+ int numOfLarge;
+ int numOfMedium;
+ int numOfTable;
+
+ public TableManager(){
+ //TODO set numbers.
+
+ }
+ public void reservResult(Request r){
+ if (processRequest(r)){
+ System.out.println("Successed!");
+
+ }
+ else{
+ "You are on the waiting list or please change your reservation information.";
+ }
+ }
+ private boolean processRequest(Request r){
+ HashMap<String, Integer> request = fitTable(r.seats);//Parse what type of request(Small, Medium, Large).
+ boolean isReserved = isAvailable(r.date, request, r.ts);
+ if (isReserved){
+ //Update resWithName, resWithDate.
+ }
+ }
+
+ private boolean isAvailable(Date date, HashMap<String, Integer> request, TimeSpan ts){
+ boolean isReserved = false;
+
+ //HashMap<String, ArrayList<Integer>> res = resWithDate.get(date);//Get a list of reserved tables at that time.
+ //ArrayList<Integer> start, end;
+ // start = res.get(ts.start);
+ // end = res.get(ts.end);
+ Iterator i = request.iterator();
+ while (i.hasNext()){
+ MapEntry temp = MapEntry(i.next());
+ if (temp.key() == "Small"){
+ int[] result = findTable("Small", temp.value());
+ }
+ else if(temp,key()=="Medium"){
+
+ }
+ else{
+
+ }
+
+ Update();
+
+ //check each request can be satified or not.
+ //zhuo zi ke yi nuo de ya hun dan !!!
+ }
+
+ }
+ private int[] findTable(String type, int num, Date date, TimeSpan ts){
+ int[] result = new int[num];
+ if (type == "Small"){
+ for (int i = 0 ; i < smallNumber; i++){
+ HashMap<String, String> res = tables[i].getReserved().get(date);
+ while (res.hasNext()&&num!=0){
+ if (isRange(res.key, res.value, ts.start, ts.end)){
+ result[num] = i;//find a table
+ num--;
+ }
+ }
+ }
+ }
+ else if(type == "Medium"){
+
+ }
+ else{
+
+ }
+
+ //Cant find a table for that.
+
+ return result;
+ }
+
+ private boolean inRange(String s1, String e1, String s2, String e2){
+ if (e1 < s2 || e2 < s1) return true;
+ else return false;
+ }
+
+ private void Update()
+}
+
+class Rinfo{
+ String name;
+ String add;
+ String phoneno;
+}
+
+class Table{
+ int tableid;
+ String type;
+ HashMap<Date, HashMap<String,String>> reserved;//e.g. Jan5 -><1100, 1200> indicated <start end>.
+ boolean isFitToRequest(TimeSpan ts){
+
+ }
+}
+
+class fitTable(){
+ HashMap<String, Integer> result = new HashMap<String, Integer>();
+ public fitTable(int x){
+ return result.add(...);
+ }
+}
+abstract class Request{
+ String name;
+ TimeSpan ts;
+ Date date;
+ int seats;
+
+ boolean canFitintoTable(Table t){
+
+ }//If it's large then maybe we need two table.
+}
+
+class TimeSpan{
+ String start;
+ String end;
+}
View
154 Trie.java
@@ -0,0 +1,154 @@
+import java.lang.String;
+
+
+public class Trie
+{
+
+ private static final int ALPH = 26;
+ private static final int BUFSIZ = 1024;
+ private Trie[] myLinks;
+ private boolean myIsWord;
+ public int count;
+
+
+ public Trie()
+ {
+ myLinks = new Trie[ALPH];
+ myIsWord = false;
+ count = 0;
+ }
+
+ public void addString(String s)
+ {
+ Trie t = this;
+ int k;
+ int limit = s.length();
+ for(k=0; k < limit; k++)
+ {
+ int index = s.charAt(k) - 'a';
+ if (t.myLinks[index] == null)
+ {
+ t.myLinks[index] = new Trie();
+ t.count = t.count + 1;
+ }
+
+ System.out.println(t.count);
+ t = t.myLinks[index];
+ }
+ t.myIsWord = true;
+ }
+
+ public void addCString(char s[])
+ {
+ Trie t = this;
+ int k=0;
+ while (s[k] != '\0')
+ {
+ int index = s[k] - 'a';
+ if (t.myLinks[index] == null)
+ {
+ t.myLinks[index] = new Trie();
+ }
+ t = t.myLinks[index];
+ k++;
+ }
+ t.myIsWord = true;
+ }
+
+ /**
+ * print every word in the trie, one per line
+ *
+ */
+
+ public void Print()
+ {
+ //DoPrint("",this);
+ apply(new Recorder());
+ }
+
+ public void apply(Recorder rec)
+ {
+ char[] buffer = new char[BUFSIZ];
+ doApply(rec,0,buffer,this);
+ }
+
+ private void doApply(Recorder rec, int index, char buffer[], Trie t)
+ {
+ if (t != null)
+ {
+ if (t.myIsWord)
+ {
+ rec.record(new String(buffer,0,index) + t.count);
+ }
+ int k;
+ for(k=0; k < ALPH; k++)
+ {
+ if (t.myLinks[k] != null)
+ {
+ buffer[index] = (char)(k+'a');
+ doApply(rec,index+1,buffer,t.myLinks[k]);
+ }
+ }
+ }
+ }
+
+
+ public boolean isWord(String s)
+ {
+ Trie t = this;
+ int k;
+ int limit = s.length();
+ for(k=0; k < limit; k++)
+ {
+ int index = s.charAt(k) - 'a';
+ if (t.myLinks[index] == null) return false;
+ t = t.myLinks[index];
+ }
+ return t.myIsWord;
+ }
+
+ /**
+ * @return true iff path from some root to this node is a word
+ *
+ */
+
+ public boolean isWord()
+ {
+ return myIsWord;
+ }
+
+ /**
+ * @param ch Character used to index node (find child)
+ * @return Trie formed from this by indexing using ch
+ */
+
+ Trie childAt(char ch)
+ {
+ return myLinks[ch-'a'];
+ }
+
+
+ public static void main(String[] args){
+ Trie myTrie = new Trie();
+ myTrie.addString("int");
+ // myTrie.addString("ant");
+ // myTrie.addString("att");
+ // myTrie.addString("bat");
+ // myTrie.addString("limit");
+ // System.out.println(myTrie.isWord("ban"));
+ myTrie.Print();
+ }
+}
+
+class Recorder
+{
+ public void record(Object o)
+ {
+ System.out.println(o);
+ }
+
+ public void report()
+ {
+ // nothing to do here
+ }
+}
View
20 div.java
@@ -0,0 +1,20 @@
+public class Div{
+
+ public static int div(int x, int y){
+ if (y == 0) return;
+ int ans = 0;
+ for (int i = 31; i >= 0;i++){
+ if ((x>>i)>=y){
+ ans += (1<<i);
+ System.out.println(ans);
+ x -= (y<<i);
+ }
+ }
+ return ans;
+ }
+
+ public static void main(String[] args){
+ System.out.println(div.div(15, 3));
+
+ }
+}
View
85 findX.java
@@ -0,0 +1,85 @@
+public class findX{
+
+ public static int find(int[] arr, int x){
+ return find(arr, x, 0, arr.length-1);
+ }
+
+ private static int find(int[] arr,int x, int left, int right){
+ if (left > right)
+ return -1;
+ int mid = (left+right) >>1;
+ System.out.println("The mid is " + arr[mid]);
+ if (arr[mid] == x)
+ return mid;
+ if (arr[mid] > arr[left])//left side is sorted normally.
+ {
+ if (x >= arr[left] && x <= arr[mid])
+ return find(arr, x, left, mid);
+ else
+ return find(arr, x, mid+1, right);
+ }
+ else if (arr[mid] < arr[left]) //Right side is sorted normally.
+ {
+ if (x>=arr[mid] && x <= arr[right]){
+ return find(arr,x,mid+1, right);
+ }
+ else{
+ return find(arr, x, left, mid-1);
+ }
+ }
+ else if (arr[mid] == arr[left]){
+ if (arr[mid] != arr[right]){
+ return find(arr, x, mid+1, right);
+ }
+ else{
+ int result = find(arr, x, left, mid-1);
+ if (result != -1){
+ return result;
+ }
+ else{
+ return find(arr, x, mid+1, right);
+ }
+ }
+ }
+ return -1;
+
+ }
+ public static void rotated(int[] arr, int d, int n){
+ int gcd = gcd(d,n);
+ int i,j,k;
+ for (i = 0 ;i < gcd;i++){
+ int temp = arr[i];
+ j = i;
+ while (true){
+ k = j+d;
+ if (k >= n)
+ k = k - n;
+ if (k == i)
+ break;
+ arr[j] = arr[k];
+ j = k;
+ }
+ arr[j] = temp;
+ }
+ }
+
+ private static int gcd(int a, int b){
+ if (b == 0) return a;
+ else return gcd(b, a%b);
+ }
+
+ public static void main(String[] args){
+ int[] a = new int[10];
+ for (int i = 0; i < a.length; i++){
+ a[i] = i;
+ }
+ findX.rotated(a, 2, 5);
+ findX.rotated(a, 2, 5);
+ System.out.println(findX.find(a, 4));
+ for(int i = 0; i < a.length;i++){
+ System.out.print(a[i]);
+ }
+
+ }
+
+}
View
30 findXinMatrix.java
@@ -0,0 +1,30 @@
+public class findXinMatrix{
+ public int[][] matrix;
+
+ public findXinMatrix(int[][] x){
+ matrix = x;
+ }
+//recursively return the element along the diagonal
+ public int partitionSearch(int[][] matrix, int start, int end, int x){
+ int lenR = matrix[0].length;
+ int lenC = matrix.length;
+ if (matrix == null || end < start) return null;
+ for (int i = 0; i < )
+ }
+ class element{
+ int row;
+ int column;
+
+ boolean isValid(int[][])
+ }
+ public static void main(String[] args){
+ int[][] matrix = { {15,20,70,85},
+ {20,35,80,95},
+ {30,55,95,105},
+ {40,80,100,120}
+ };
+ findXinMatrix fxm = new findXinMatrix(matrix);
+
+ System.out.println(fxm.matrix[0].length);
+ }
+}
View
BIN mitbbs jobhutting 2011-03.pdf
Binary file not shown.
View
42 power.java
@@ -20,6 +20,7 @@ public static double power( double x , int n )
bitMask >>>= 1;
while ( bitMask != 0 )
+ //System.out.println(bitMask);
{
evenPower *= evenPower;
if ( (bitMask & 1) != 0 )
@@ -32,25 +33,25 @@ public static double power( double x , int n )
return result;
} // end power
-public static int bitLog( int n )
- {
- if ( n <= 8 )
- {
- return 2 * n;
- }
- else
- {
- int b = 31;
- while ( b > 2 && n > 0 )
- {
- b--;
- n <<= 1;
- }
- n &= 0x70000000;
- n >>= 28;
- return n + 8 *( b - 1 );
- }
- } // end bitLog
+// public static int bitLog( int n )
+// {
+// if ( n <= 8 )
+// {
+// return 2 * n;
+// }
+// else
+// {
+// int b = 31;
+// while ( b > 2 && n > 0 )
+// {
+// b--;
+// n <<= 1;
+// }
+// n &= 0x70000000;
+// n >>= 28;
+// return n + 8 *( b - 1 );
+// }
+// } // end bitLog
public static double root(double num, double x){
int radix = (int)(1/x);
System.out.println(radix);
@@ -77,8 +78,7 @@ public static double root(double num, double x){
return result;
}
public static void main(String[] args){
- System.out.println(power.power(-5,2));
- System.out.println(power.root(5, 0.5));
+ System.out.println(power.power(5.0,2));
}
}
View
60 power2.java
@@ -0,0 +1,60 @@
+public class power2{
+
+ public static int power(int x, int y){
+ int result=x;
+ while (y != 1){
+ result *= result;
+ y--;
+ }
+ return result;
+
+ }
+ public static int power2( int x , int n )
+ {
+ int bitMask = n;
+ int evenPower = x;
+ int result;
+ if ( (bitMask & 1) != 0 )
+ {
+ result = x;
+ }
+ else
+ {
+ result = 1;
+ }
+ bitMask >>>= 1;
+
+ while ( bitMask != 0 )
+ {
+ System.out.println(bitMask);
+ evenPower *= evenPower;
+ if ( (bitMask & 1) != 0 )
+ {
+ result *= evenPower;
+ }
+ bitMask >>>= 1;
+
+ } // end while
+ return result;
+ } // end power
+
+ public static void main(String[] args){
+ long start = System.nanoTime();
+
+ // System.out.println(power2.power2(5,4));
+ long end = System.nanoTime();
+ int sum1= 9;int sum2 = 5;
+ int result = 0;
+ int c = 0;
+ while (true){
+ c = (sum1 & sum2);
+ System.out.println(c);
+ sum1 = sum1^sum2;
+
+ sum2 = c << 1;
+ System.out.println(sum1 + "!!"+sum2);
+ if (c == 0){break;}
+ }
+ System.out.println(sum1^sum2);
+ }
+}
View
20 qsort1.py
@@ -0,0 +1,20 @@
+def qsort1(list):
+ """
+ Quicksort using list comprehensions
+ >>> qsort1<<docstring test numeric input>>
+ <<docstring test numeric output>>
+ >>> qsort1<<docstring test string input>>
+ <<docstring test string output>>
+ """
+ if not list:
+ return []
+ else:
+ pivot = list[0]
+ lesser = qsort1([x for x in list[1:] if x < pivot])
+ greater = qsort1([x for x in list[1:] if x >= pivot])
+ return lesser + [pivot] + greater
+
+s=[98,56,23,15,69]
+s2 = []
+s2 = qsort1(s)
+print s2
View
11 reverse.py
@@ -0,0 +1,11 @@
+ def reverseString(s):
+ if (s == ""):
+ return s
+ else:
+ return reverseString(s[1:])+s[0]
+
+
+print reverseString("abcd")
+t = "abcd"
+backward = lambda t: t[-1] + backward(t[:-1]) if t else t
+print backward(t)
View
15 swap.py
@@ -0,0 +1,15 @@
+#!/usr/bin/python
+
+a = 6
+b = 7
+a = a+b
+b = a-b
+a = a-b
+print a, b
+
+a^=b
+print a,b
+b^=a
+print a,b
+a^=b
+print a,b
View
33 temp/findPath.java
@@ -0,0 +1,33 @@
+import java.util.*;
+public class findPath{
+
+
+
+
+ public static void getPath(int size, int x, int y, ArrayList paths){
+ if (x== size && y == size){
+ Iterator i = paths.iterator();
+ while (i.hasNext()){
+ System.out.print(i.next()+"|");
+ }
+ System.out.println();
+ //paths.clear();
+ }
+ if (x < size){
+ paths.add('D');
+ getPath(size, x+1,y,paths);
+ paths.remove(paths.size()-1);
+ }
+ if (y < size){
+ paths.add('R');
+ getPath(size, x,y+1,paths);
+ paths.remove(paths.size()-1);
+
+ }
+ }
+
+ public static void main(String[] arg){
+ ArrayList al = new ArrayList();
+ findPath.getPath(1,0,0,al);
+ }
+}
View
59 temp/qipansuoyoujiaoluo.c
@@ -0,0 +1,59 @@
+void GetPath(int size, int x,int y,vector<char>& vec)
+{
+if(x==size&&y==size)
+{ vector<char>::iterator it;
+ for(it=vec.begin();it!=vec.end();++it) { cout<<*it; }
+ cout<<endl;
+ return;
+}
+if(x<size) {
+ vec.push_back('R');
+ GetPath(size,x+1,y,vec);
+ vec.pop_back();
+ }
+if(y<size) {
+ vec.push_back('D');
+ GetPath(size,x,y+1,vec);
+ vec.pop_back();
+ }
+return;
+}
+
+参照网上的思路,写了一个wildcard match。
+做面试用还算简洁。谁给Review一下?
+
+#include <iostream>
+using namespace std;
+bool match(char* in, int iidx, char* ptn, int pidx)
+{
+ if (!in || !ptn) return false;
+
+ // base case
+ if (!in[iidx] && !ptn[pidx]) return true;
+ if (!ptn[pidx]) return false;
+ if (!in[iidx]) {
+ while(ptn[pidx]) if (ptn[pidx++]!='*') return false;
+ return true;
+ }
+
+ if (ptn[pidx]=='?' || ptn[pidx]==in[iidx])
+ return match(in, iidx+1, ptn, pidx+1);
+ if (ptn[pidx]=='*') {
+ while (in[iidx]) {
+ if (match(in, iidx++, ptn, pidx+1)) return true;
+ }
+ while(ptn[++pidx]) if (ptn[pidx]!='*') return false;
+ return true;
+ }
+ return false;
+}
+
+int main()
+{
+ char input[] = "regular expression";
+ char pattern[] = "?egu*pr??*o*";
+ cout << input << " => " << pattern << " : " <<
+ match(input, 0, pattern, 0) << endl;
+ cout << "ab" << " => " << "a" << " : " <<
+ match("ab", 0, "a", 0) << endl;
+}

0 comments on commit ae06d53

Please sign in to comment.