Wednesday, September 18, 2013

[LeetCode]Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

Idea: use hashtable to effectively search item, search complexity is o(1) and use linkedlist to avoid collision



杂记--身体抱恙


清晨,迷迷糊糊地从梦中起来,感觉身体已无大碍,昨夜发烧的额头已没有了滚烫的感觉,身体感觉有些乏力,但是已然没有了昨晚全身酸痛发烧的感觉。多亏臭臭的细心照顾,又熬姜汤又搓胳膊的,才能这样快的康复。想想生病的原因,也许是前段时间事业受阻,亦或是工作的地方空调开得过于凶猛,也就稀里糊涂的病了。

九月末佛州的早上,空气中夹杂着一股凉意,不冷不热的,让我想起家里的感觉。那时候,这么早起来还属于学生的时代,每天要坐一段公交车。而从家里走到车站的五分钟里,就能感受到这股凉意,沁人心脾。

这段时间有些迷惘,有些失落,不知今后回头看自己的现在,是何种滋味。。

还好有臭臭陪着,加油,我们一起~


Tuesday, September 17, 2013

Embed Code with Blogger Tutorial


SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript.

You can find more detail about SyntaxHighlighter at http://alexgorbatchev.com/SyntaxHighlighter/

SyntaxHighlighter provides a broad syntax library for users to highlight their code.


Here is a screen shot on how many language does current version support:




The idea is pretty straightforward, you just need to link the .js file in your blog, and then simply tell the blog about which part you want to show as Code. Want to know how to do it? I will walk you through on how to create a blog template, create embedded code page and apply for different theme.


Now, let's start:


First, We need to select the Edit HTML.



Under Edit HTML,  find the tag </head>, you need to put SyntaxHighlighter brush library here.

Copy and pasta following code:


    
    


After Saving the the template, you have done the first step!


Then, you can create your new post as usual.  


Next, switch your view from compose to HTML view



Put the code you want, for simplicity, I will do the following code:



/**
     * SyntaxHighlighter
     */
    function foo()
    {
        if (counter <= 10)
            return;
        // it works!
    }

It will show like


/**
     * SyntaxHighlighter
     */
    function foo()
    {
        if (counter <= 10)
            return;
        // it works!
    }

Now you have done pretty much of all of them!

Finally, you may not like the theme of your code. You can find all the theme here.
All you need to do is go back to the first step, edit HTML, Find one line of code like:

Change to the theme you would like to use, for example:


Now you can create the embedded code HTML yourself, enjoy!

I hope this blog will be helpful!

Tuesday, May 14, 2013

生命如此短暂,你仍走的如此匆忙

纪念我们笑过,哭过,奋斗过的曾经。。。

清晨,阳光将我唤醒,我习惯性的翻看手机,微信讨论组里突然出现了一位初中好友去世的消息。我极度不相信,不断地向上去翻看之前的留言去找寻玩笑之类的消息。直到……再也翻不上去,脑子“嗡”地一下炸开了,脑海中仿佛有千百思绪,却无论如何也抓不住其中任何一条,我恍惚着看着手机,不断推送过来的信息证明了这一点,他已经离开了我们,永远的失去了消息。

印象中的你,和蔼可亲,幽默感是你的主旋律,和你为伴总是笑声不断。没想到,两年前去北京办事,我们当时的一聚成为了阴阳相隔的分别。



此时此刻,为你默默地在地球的另一端送上我的祝福,

生命如此短暂,你仍走的如此匆忙,

一路走好

Wednesday, April 10, 2013

Merge Sort Recursive


public class Merge {
    public int [] data;
    private int [] helper;
   
   
    public Merge(int[] a){
        data=a;
        this.helper = new int[a.length];
    }
   
    public void splitAndMerge(int begin, int end){
       
        if(begin<end){
            int middle = (begin+end)/2;
            splitAndMerge(begin, middle);
            splitAndMerge(middle+1, end);
            MergeSort(begin, middle, end);
        }
    }
   
    public void MergeSort(int begin, int middle, int end){
         for(int i=begin;i<=end;i++){
             helper[i]=data[i];
         }
   
         int x=begin;
         int y=middle+1;
         int z=begin;
       
         while(x<=middle&&y<=end){
             if(helper[x]<=helper[y]){
                    data[z]=helper[x];
                    x++;
                    z++;
             }
             else{
                     data[z]=helper[y];
                     y++;
                     z++;
             }
         }
       
         //Copy the rest of left to the array
         while(x<=middle){
             data[z]=helper[x];
             z++;
             x++;
         }
    }
}

Friday, April 5, 2013

[Introduction to Algorithms] Merge Sort Example

Book - [Introduction to Algorithms]
Page 31

Divide-and-conquer approach: they break the problem into several subproblems that are similar to the original problem but smaller in size, solve the subproblems recursively, and then combine these solutions to create a solution to the original problem.

Coded in Java
Merge sort example
O(n)

public static void main(String[] args) {
        int [] data={99,7,3,4,6,23,34,5,2,100};
        int [] newdata = MergeSort.mergeSort(data, 1, 4, 8);
        for(int i=0;i<newdata.length;i++){
            System.out.print(newdata[i]+" ");
        }
    }
public static int[] mergeSort(int[] data, int p, int q,int r){
        int n1 = q-p+1;
        int n2 = r-q;
        int [] L = new int[n1+1];
        int [] R = new int[n2+1];
        int index = p ;



//Sort Left
        for(int i=0;i<L.length;i++){
            L[i]=data[p];
            p++;
        }
        Arrays.sort(L);
        L[n1]=Integer.MAX_VALUE;

//Sort Right       
        for(int i=0;i<R.length;i++){
            R[i]=data[q+1];
            q++;
        }
        Arrays.sort(R);
        R[n2]=Integer.MAX_VALUE;
       
//Here comes the merge of left and right
         for(int i=0, j=0;index<r+1;){
            if(L[i]>=R[j]){
                data[index++]=R[j++];
            }
            else if(L[i]<R[j]){
                data[index++]=L[i++];
            }
        }
        return data;
     }