Monday, September 22, 2014

How to install Elasticsearch on Windows

Installation:

1. Download elasticsearch from website: (.zip file for windows)
http://www.elasticsearch.org/overview/elkdownloads/

2. Unzip the file, and find the /bin directory, for example:
C:\Users\Quan\Downloads\elasticsearch-1.3.2\elasticsearch-1.3.2\bin

3. Open up your command line, and get into the /bin directory, for example:
cd C:\Users\Quan\Downloads\elasticsearch-1.3.2\elasticsearch-1.3.2\bin

4. And then run file "elasticsearch.bat" in the command line,
elasticsearch.bat

You will see this:

 5. Finally, browse this URL in your browswer:
http://localhost:9200/

If you see this code, then you have successfully start your elasticsearch server!
{
  "status" : 200,
  "name" : "Quan",
  "version" : {
    "number" : "1.3.2",
    "build_hash" : "dee175dbe2f254f3f26992f5d7591939aaefd12f",
    "build_timestamp" : "2014-08-13T14:29:30Z",
    "build_snapshot" : false,
    "lucene_version" : "4.9"
  },
  "tagline" : "You Know, for Search"
}

How to install Solr on Windows

First, you need to install Java on your machine, if you don't have it, install it first.
After installing Java.........

1. Download the Solr from this website:
https://lucene.apache.org/solr/downloads.html

2. Unzip the downloading file..and open up the "example" folder.

3. Open up the command line, cd the "example" folder in command line.
For example:






4. Now in the command line, type in "java -jar start.jar":
java -jar start.jar
5. This will start jetty webserver and deploy solr automatically, and now open up your browser and browse URL: http://localhost:8983/solr.

6. And then you can see the solr admin GUI panel.

Thursday, September 18, 2014

Validate Binary Search Tree


Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • The left subtree of a node contains only nodes with keys less than the node's key.
  • Both the left and right subtrees must also be binary search trees.
Code in JAVA:
public class Validate_binary_search_tree {
     public class TreeNode{
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode(int x) {
              val = x;
         }
     }
 int lastVal = Integer.MIN_VALUE;
 public boolean isValidBST(TreeNode root) {
  
        if(root == null) {
         return true;
        }
        if (!isValidBST(root.left)) {
         return false;
        }
        if (lastVal >= root.val) {
         return false;
        }
        lastVal = root.val;
        if (!isValidBST(root.right)) {
         return false;
        }

        return true; 
    }
} 

Tuesday, September 9, 2014

Minimum Depth of Binary Tree

/*
 * time complexity is O(n), because every node visit once, is it right?
 */
public class Minimum_depth_of_binary_tree {
 
 public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int x) { val = x; }
 }
 public int minDepth(TreeNode root) {
  //shortest path form the root node down to the nearest leaf node
  if (root == null) {return 0;}

  return getMin(root);
    }
 
 public int getMin(TreeNode root) {
  if (root == null) {
   return Integer.MAX_VALUE;
  }
  if (root.right == null && root.left == null) {
   return 1;
  }
  return Math.min(getMin(root.left), getMin(root.right)) + 1;
 }
}

Wednesday, September 3, 2014

Interleaving String-1

// 没法考虑两个字符串的组合顺序问题。当处理{"aa", "ab", "aaba"}的时候,就不行了
// Merge sort
public class Interleaving_string {
 public static boolean isInterleave(String s1, String s2, String s3) {
        
        if (s3.length() != s2.length() + s1.length()) {
         return false;
        }
        int index = 0; //s3
        int n = 0;     //s1
        int m = 0;     //s2
        while(n < s1.length() && m < s2.length()) {
         if (s3.charAt(index) == s1.charAt(n)) {
          n++;
         } else if (s3.charAt(index) == s2.charAt(m)) {
          m++;
         } else {
          return false;
         }
         index++;
        }
        while (n < s1.length()) {
         if (s3.charAt(index) == s1.charAt(n)) {
          n++;
         } else {
          return false;
         }
         index++;
        }
        while (m < s2.length()) {
         if (s3.charAt(index) == s2.charAt(m)) {
          m++;
         } else {
          return false;
         }
         index++;
        }
 
        return true;

    }
 
}

Reverse words in a String

/*
 * tricky part: the last append(" ") needs to be deleted if this is the last word
 */
public class Reverse_words_in_a_string {
 public static String reverseWords1(String s) {
  if (s == null || s.length() == 0) {
   return "";
  }
  String[] arrayStr = s.split(" ");
  int length = arrayStr.length;

     StringBuffer newStr = new StringBuffer();
  for (int i = length - 1; i >= 0; i--) {
   if (!arrayStr[i].trim().equals("")) {
    newStr.append(arrayStr[i].trim()).append(" ");
   }
   
  }
  // the last " "(space) needs to be deleted
  return newStr.length() == 0 ? "": newStr.substring(0, newStr.length() - 1);

    }