google code prettify

2016年7月16日 星期六

LeetCode - 371. Sum of Two Integers

Question:

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:
Given a = 1 and b = 2, return 3.

Solution:


public class Solution {
    public int getSum(int a, int b) {
        return a+b;
    }
}

LeetCode - 374. Guess Number Higher or Lower

Question:

We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I'll tell you whether the number is higher or lower.
You call a pre-defined API guess(int num) which returns 3 possible results (-11, or 0):

Solution:


public class Solution extends GuessGame {
    
    public int guessNumber(int n) {
        Long R = new Long(n);
  Long L = 1L;
   
  if(R!=L){
   while(R>=L){
    Long mid = ((L+R)>>1);

    int result = guess(mid.intValue());
    //System.out.println("mid:" + mid + " ,result:" + result);
    if(result ==1){
     L = mid+1;
     
    }else if(result ==-1){
     R = mid-1;
    }else{
        return mid.intValue();
    }
   }
  }else{
      return R.intValue();
  }
  
  
  return L.intValue();
  
 }
}

LeetCode - 20. Valid Parentheses

Question:

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Solution:


public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
  
  char[] array = s.toCharArray();
  if (array.length % 2 != 0) {
   return false;
  }
  
  for(int i =0;i<array.length;i++){
   char c = array[i];
   if(c=='(' || c=='[' || c=='{'){
    stack.push(c);
    continue;
   }
   
   if(stack.isEmpty()==true){
    return false;
   }
   
   if(c==')'){
    if (stack.pop()!='(')
     return false;
   }else if(c==']'){
    if(stack.pop()!='[')
     return false;
   }else if(c=='}'){
    if(stack.pop()!='{')
     return false;
   }else{
    return false;
   }
  }
  
  if(stack.isEmpty()==false){
   return false;
  }else{
   return true;
  }
    }
}

2016年7月2日 星期六

在Mac上安裝Docker

關於Docker就不多說了,Docker現在出了Docker for Mac Beta
因此就不用再使用Docker Toolbox了
下載位置:https://docs.docker.com/docker-for-mac/

1、安裝非常簡單,從左邊拉到右邊就好了


2、安裝後執行,就可以在右上角看到執行中的圖示


















3、就開始開終端機開始使用docker了,用「docker --version」確認版本資訊






4、安裝nginx,輸入指令「docker run -d -p 80:80 --name webserver nginx」



5、打開瀏覽器輸入「http://localhost/」就可以看到已經建立nginx服務了












Reference:
https://docs.docker.com/docker-for-mac/

2016年3月19日 星期六

運用Jsoup取得網頁資訊

如果要使用Java去parse Web page, 可以使用Jsoup,簡單好用
以下範例為使用jsoup抓取yahoo 首頁的新聞標題



package test.charles.JsoupExample;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class App 
{
    public static void main( String[] args ) throws IOException
    {
     //read yahoo home page
     String url  = "https://tw.yahoo.com/";
     Document doc = Jsoup.connect(url).get();
     
     //get first news tab one
     Element t1 = doc.getElementById("t1");
     
     //get news title
     Elements newsTitle = t1.select("a[href] > span");
     
     //print size
        System.out.println("size:" + newsTitle.size());
        
        //print news title
        for(Element e:newsTitle){
         System.out.println("title:" + e.text());
        }
        
    }
}




程式碼可以至github下載

2016年3月16日 星期三

在Mac上安裝Cordova

當想要快速開發一個簡單的app又不會寫Android及iOS原生程式的話
我很推薦使用Cordova
這是一種使用Html + Javascript + CSS來模擬app,對於網頁工程師並沒什麼門檻 以下範列為在Mac上安裝Cordova,並建立範例的Android project

 1、透過nodejs安裝cordova
輸入「sudo npm install cordova -g」
-g代表將此套件安裝在全域的資料夾中


2、檢查是否安裝成功
輸入「cordova --version」



3、切換到資料夾下建立Cordova專案
輸入「sudo cordova create hello」



此時可以看到成功產生出Cordova的專案



4、切到目錄下產生Android的專案
輸入「sudo cordova platforms add android」



5、之後再到platforms下檢查就可以看到android的專案嚕


結束!!

2016年3月3日 星期四

Java Collections.sort

當使用自訂義的類別想要排序時,可以採用Collections.sort
範例如下



package test.charles.CollectionSort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class App 
{
    public static void main( String[] args )
    {
        List userList = new ArrayList();
        //add user name and age
        userList.add(new User("user1",19));
        userList.add(new User("user2",40));
        userList.add(new User("user3",3));
        userList.add(new User("user4",27));
        userList.add(new User("user5",10));
        userList.add(new User("user6",70));
        
        //sort by user age
        Collections.sort(userList, new Comparator() {
   public int compare(User o1, User o2) {
    return o1.getAge().compareTo(o2.getAge());
   }
  });
        
        //print information
        for(User s :userList){
         System.out.println("name:" + s.getName() + ",age:" + s.getAge());
        }
    }
}




程式碼可以至github下載