標籤:最大堆的性質是除了根節點之外的所有節點(i)都需要滿足A[PARENT(i)]>A[i],即其對應節點值小於其父節點對應值。下面實現以數組int []a構建最大堆。 public class Heap {public static int Left(int i)//返回左子結點{return 2*i+1;}public static int Right(int i)//返回右子節點{return 2*i+2;}public static void
標籤:內容:一個簡單的死結例子,大概的思路:兩個線程A和B,兩把鎖X和Y,現在A先拿到鎖X,然後sleep()一段時間,我們知道sleep()是不會釋放鎖資源的。然後如果這段時間軸程B拿到鎖Y,也sleep()一段時間的話,那麼等到兩個線程都醒過來的話,那麼將互相等待對方釋放鎖資源而僵持下去,陷入死結。flag的作用就是讓A和B獲得不同的鎖。public class TestDeadLock {public void run() {MyThread mt = new
標籤:Implement int sqrt(int x).Compute and return the square root of x.解題思路一: public int mySqrt(int x) { return (int)Math.sqrt(x); } 神奇般的Accepted。解題思路二:參考平方根計算方法 計算平方根的演算法這裡給出最簡單的牛頓法,JAVA實現如下: public int mySqrt(int x) {
標籤:Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad
標籤:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?解題思路:遞迴會導致逾時,用動態規劃即可,JAVA實現如下; public int climbStairs(int n) { if(n==1)
標籤:最近在用eclipse玩的時候,建立項目時什麼都貪新,用最新的版本,在Dynamic web module version欄裡選了最新的3.0版本,布署項目的時候就出現了如期的錯誤,在網上看到其他網友的部落格,解決了問題,現將方法貼於此,希望對別人有所協助:通過search,發現在project的.setting