Doing something you intrinsically are enthusiastic with.

2016年3月26日 星期六

Leecode-Minimum Path Sum in Java

上午9:42 Posted by Unknown No comments
題目

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
----
It's a traditional Dynamic programming problem.
All we have to do is to choose the small sum in each time then record all the entries.
So, we may have a TWO-DIMENSION ARRAY to store the value 
and the answer will be at entry[m-1][n-1] .

However, it's a waste of the memory since we don't need to save all the values in the array.
So, we can use the REFACTOR-ARRAY to reuse the value and record every row.
The answer will be the last element in the last row computation.





public class Solution {


   public int minPathSum(int[][] grid) {
     
     if (grid == null || grid.length == 0 || grid[0].length == 0)
            return 0;

          int[] temp = new int[grid[0].length]; // Refactor array
        
          for (int i = 0; i < grid.length; i++)
            for (int j = 0; j < grid[0].length; j++) {
                if (j > 0)
      
                  if (i > 0)
                    temp[j] = Math.min(temp[j], temp[j - 1]);  
     // temp[j] represents a[i-1][j] , temp[j-1] represents a[i][j-1] 
                  else
                    temp[j] = temp[j - 1];
                
                temp[j] += grid[i][j];
            }
        
     //The computation of first row and the first column are simple
    //Java initiate all the elements to zero 
    // so we just need to add to get the correct value.
        
            
        
          return temp[temp.length - 1];   //answer's here
    }


}

2016年3月17日 星期四

Difference and uses of onCreate(), onCreateView() and onActivityCreated()

上午10:15 Posted by Unknown No comments

onCreate():
The onCreate() method in a Fragment is called after the Activity's onAttachFragment() but before that Fragment's onCreateView().
In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the Activity's onCreate() is not finished, and so trying to access the View hierarchy here may result in a crash.
onCreateView():
After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View to this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null.
onActivityCreated():
As the name states, this is called after the Activity's onCreate() has completed. It is called after onCreateView(), and is mainly used for final initialisations (for example, modifying UI elements).

To sum up...
They are all called in the Fragment but are called at different times.
The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView(). Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.


2016年3月12日 星期六

struct 與 Class 的差別在哪裡?

清晨5:56 Posted by Unknown No comments


有兩種情況下的區別

(1)C的struct與C++的class的區別
(2)C++中的struct和class的區別



第一種情況下面很簡單

C是一種Procedural Programming的語言

在C裡面,Struct只是來作為一種複雜數據的集合定義

只能定義成員變數,不能定義成員函數

但是在C++裡面,struct就可以定義成員函數



至於第二種的情況則是

雖然在C++中的struct可以定義成員函數,也可以做繼承

但是struct的繼承預設為public,而class中的繼承是private

class可以被使用於Template當中但struct不行


在c++中的stuct就只是為了使C++能夠兼容C而已

2016年3月6日 星期日

Leecode Generate Parentheses in Java

凌晨4:47 Posted by Unknown No comments
題目:

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"


這個題目如果是問個數的話,那就是Catalan Number的一種了

不過這一題沒那麼簡單,是要你把所有的Pattern都生出來

簡單的畫一下Recursion tree就大概知道了

左括號的數目一定要 >=右括號的數目

重點應該是在於,我怎麼要讓程式自動在一個左括號之後

能夠自動去選擇我下一個要是左括,或者是右括號

這樣才能生成所有想要的結果。


所以在這裡會有分歧點

需要一個if來判斷現在是左括號還有quota可以用

另一個if來判斷現在是不是還有右括號,而且右括號數目比左括號多

這兩種狀況就能涵蓋所有的結果






public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        
    if(nums==null)
        return null;
        
    if(nums.length==1)
        return new TreeNode(nums[0]);
    
    TreeNode node = BuildTree(0,nums.length-1,nums);
    return node;

    }
    
    TreeNode BuildTree(int start,int end, int[]nums){
        
        if(start>end)
            return null;
        
        int mid=(start+end)/2;
        TreeNode node=new TreeNode(nums[mid]);
        
        node.left=BuildTree(start,mid-1,nums);
        node.right=BuildTree(mid+1,end,nums);
        
        return node;
     }
    
    
}