Thursday, December 11, 2014

Binary Search - Java


public int binarySearch(int A[], int target) {
  int start = 0;
  int end = A.length - 1;
  while (start <= end) {
   int mid = (end - start) / 2 + start;
   if (target < A[mid]) {
    end = mid - 1;
   } else if (target > A[mid]) {
    start = mid + 1;
   } else {
    return mid;
   }
  }
  return -1;
  
 }
Tips of boundary:

终止条件:i <= j,  i < j
mid的上下取值:  i+(j-i)/2, j-(j-i)/2
分半的时候取:mid, mid-1, or mid+1

1. 如果终止条件i <= j, mid取向为 i + ( j - i ) / 2, 分半的时候取 mid-1 or mid+1;
2. 如果终止条件i < j, mid取向两种都需要用到,分半的时候需要用到 mid;一般取mid的时候,终止条件往往是i < j


Monday, December 8, 2014

Mac Folder System Tips

It's interesting as well as a little bit confusing when you change your operation system from Windows to Mac OS X, so I want to write down some tips to classify functions of each folder.

1. The Beginning: -> /

In OS X, the top level of the hard drive is called /. It's just a single slash. Your Mac displays "Macintosh HD".

Inside / are several folders. /Library and /System are important to the inner workings of OS X. Do not change them unless you know what you are doing.

2. Self-Explantory: -> /Applications

New installed software will be dragging in this folder.

3. Manager all Users Files: -> /Users

An OS X machine may have several individuals user accounts on it. So inside the Users folders will save accounts information.

4. Manager your Files: -> /Users/<yourname>

Every user has their own home folder. This is the folder where you can put your documents, pictures, music, etc. There are several folders already in your home folder by default: 

  •  /Users/<yourname>/Desktop 
Anything in this folder will be shown on your desktop 

  •  /Users/<yourname>/Documents
This is a good place to keep documents you create

  • /Users/<yourname>/Library
This contains information specific to your account that OS X needs. Only modify or put things in this folder if you know what you're doing.

  • /Users/<yourname>/Movies
A good place to store movies you create or download.

  • /Users/<yourname>/Music
A good place to store music. If you use iTunes and use the default "Keep iTunes Music Folder Organized" option iTunes will create the /Users/yourname/Music/iTunes folder.

  • /Users/<yourname>/Pictures
A folder for storing your pictures. If you use iPhoto it will keep its photo library in a folder inside /Users/yourname/Pictures.


  • /Users/<yourname>/Public
Anything inside this folder will be visible to the other user accounts on your computer, and also to other people on your network

Thursday, December 4, 2014

Hashtable vs HashMap vs HashSet

Hashtable
1. Retain values of key-value pair
2. Didn’t allow null for both key and value, and will get NullPointerException if add null value
3. It is synchronized, so it comes with its cost. Only one thread can access in one time
Hashtable table = new Hashtable();
table.put(1, "Beijing");
table.put(2, "Boston");
table.put(3, null); /* NullPointerException at runtime*/
 
System.out.println(table.get(1));
System.out.println(table.get(2));
System.out.println(table.get(3)); 

HashMap
1. Retain values of key-value pair
2. It allows null for both key and value
3. It is unsynchronized, so come up with better performance
HashMap map = new HashMap();
map.put(1, "Keys");
map.put(2, null);
  
System.out.println(map.get(1));
System.out.println(map.get(2));
HashSet
1. Retain object, doesn't store key-value pairs
2. Does not allow duplicate values, which means HashSet can be used where you want to maintain a unique objects
HashSet set = new HashSet();
set.add ("CA");
set.add ("MA");
set.add ("PA");
  
if (set.contains("MA")) {/* if MA, it will not add but shows following message*/
     System.out.println("Already found");
} else {
    set.add("NY");
}