Android-leap-code-01

##河內塔
public static void Move(int disks, int from, int to)
{
if(disks == 1)
{
System.out.println(“Move from “ + from + “ to “ + to);
return;
}
int relay = 6 - from - to;
Move(disks - 1, from, relay);
Move(1, from, to);
Move(disks - 1, relay, to);
}
##Fibonacci

public class DivideAndConquer {
    public static long Fibonacci(int n)
    {
        if (n == 0)
            return 0;
        if (n == 1)
            return 1;
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
}

##Merge sort
###TopDown
public class TopDown {
public static void Sort(int[] array)
{
int[] workArray = new int[array.length];
Sort(array, workArray, 0, array.length);
}

   private static void Sort(int[] array, int[] workArray, int start, int count) 
   { 
       if (count < 2) 
           return; 

       Sort(array, workArray, start, count / 2); 
       Sort(array, workArray, start + count / 2, count - count / 2); 
       Merge(array, workArray, start, count / 2, start + count / 2, count - count / 2); 
} 

 private static void Merge(int[] array, int[] workArray, int leftStart, int leftCount, int rightStart, int rightCount) 
{ 
    int i = leftStart, j = rightStart, leftBound = leftStart + leftCount, rightBound = rightStart + rightCount, index = leftStart; 
   while (i < leftBound || j < rightBound) 
   { 
       if (i < leftBound && j < rightBound) 
       { 
           if (array[j] < array[i]) 
               workArray[index] = array[j++]; 
           else
               workArray[index] = array[i++]; 
       } 
       else if (i < leftBound) 
           workArray[index] = array[i++]; 
       else
           workArray[index] = array[j++]; 
       ++index; 
   } 
   for (i = leftStart; i < index; ++i) 
       array[i] = workArray[i]; 
   }
}

##BottomUp

public class BottomUp {
    public static void Sort(int[] array) 
    { 
        int[] workArray = new int[array.length]; 

        for (int count = 1; count < array.length; count *= 2) 
            for (int leftStart = 0; leftStart < array.length; leftStart += 2 * count) 
            { 
                if (count > array.length - leftStart) 
                    break; 
                Merge(array, workArray, leftStart, count, leftStart + count, Math.min(count, array.length - leftStart - count)); 
            } 
    } 

    private static void Merge(int[] array, int[] workArray, int leftStart, int leftCount, int rightStart, int rightCount) 
    { 
        int i = leftStart, j = rightStart, leftBound = leftStart + leftCount, rightBound = rightStart + rightCount, index = leftStart; 
        while (i < leftBound || j < rightBound) 
        { 
            if (i < leftBound && j < rightBound) 
            { 
                if (array[j] < array[i]) 
                    workArray[index] = array[j++]; 
                else
                    workArray[index] = array[i++]; 
            } 
            else if (i < leftBound) 
                workArray[index] = array[i++]; 
            else
                workArray[index] = array[j++]; 
            ++index; 
        } 
        for (i = leftStart; i < index; ++i) 
            array[i] = workArray[i]; 
    }
}

##Quick Sort

import java.util.List;
import java.util.Random;
import java.util.ArrayList;

public class ObjectOriented {
    static Random random = new Random();

    public static void Sort(int[] array) {
        List list = new ArrayList();
        for (int n : array)
            list.add(n);
        list = Sort(list);
        for (int i = 0;i < array.length;++i)
            array[i] = list.get(i);
    }

    public static List Sort(List list)
    {
        if (list.size() < 2)
            return list;

        // random pivot
        //int pivot = list.get(random.nextInt(list.size() - 1));

        // middle pivot
        int pivot = list.get(list.size() / 2);
        list.remove(list.size() / 2);
        List less = new ArrayList();
        List greater = new ArrayList();
        List result = new ArrayList();
        for (Integer n : list)
        {
            if (n > pivot)
                greater.add(n);
            else
                less.add(n);
        }
        result.addAll(Sort(less));
        result.add(pivot);
        result.addAll(Sort(greater));
        return result;
    }
}

##Binary Search
public class DivideAndConquer {
static public int Search(int[] array, int num)
{
return Search(array, num, 0, array.length - 1);
}

    static public int Search(int[] array, int num, int left, int right)
    {
        if (left > right)
            return -1;

        int middle = (right + left) / 2;

        if (array[middle] == num)
            return middle;

        if (array[middle] > num)
            return Search(array, num, left, middle - 1);

        return Search(array, num, middle + 1, right);
    }
}

##下樓梯
public class Iterative {
public static long DownStairs(int n)
{
long v1 = 1;
long v2 = 2;
long result = n;
for (int i = 2; i <= n; ++i)
{
result = v2 + v1;
v1 = v2;
v2 = result;
}
return result;
}
}

##總和遞迴
int sum(int n)
{
if(n < 1)
return 0;
return n + sum(n - 1);
}

Android-Thread-Save Singleton

Android Thread-Save Singleton

public class ThreadSafeSingleton {

    private static final Object instance = new Object();

    protected ThreadSafeSingleton() {
    }

    // Runtime initialization
    // By defualt ThreadSafe
    public static Object getInstance() {
        return instance;
    }
}

Android-shrinkTextView

自動縮小字型的TextView

private void shrinkText(final TextView wv, final String word) {
    wv.setTextSize(defaultTextSize);
    new Thread(new Runnable() {
        @Override
        public void run() {
                while (wv.getWidth()>0 && wv.getPaint().measureText(word) > wv.getWidth()) {
                Logg.d("word too big. Shrink from " + wv.getTextSize()/density + " to " + (wv.getTextSize()/density-1.0f));
                wv.post(new Runnable() {
                   public void run() {
                      wv.setTextSize(wv.getTextSize() / density - 1.0f);
                   }
               });
        }
    }).start();
}