2018-07-02

Q1: 蓄水池算法pick 1
Q2: reverse linked list
Q3: reverse linked list per k nodes
Q4: BST preOrder iteration
Q5: BST inOrder iteration
Q6: BST postOrder iteration
Q7: suffle array
Q8: move negative to the end
Q9: maximum on binary tree
Q10: first Occurency bianry search

//
public class Solution {
  public int firstOccur(int[] array, int target) {
    // Write your solution here
    if (array == null || array.length == 0) {//== =
    return -1;
    }
    int i = 0;
    int j = array.length - 1;
    
    while (i < j){
        int m = (j - i) / 2 + i;
      if (array[m] == target) {
        j = m;
      } else if (array[m] > target) {
        j = m - 1;
      } else {
        i = m + 1;
      }
    }
    if (j >= 0 && array[j] == target) {
        return  j;
    }
    return -1;

  }
}

Q:
public ListNode getRandom(ListNode root) {
  int counter = 0;
  ListNode result = root;
  while (root != null) {
    if (Random.nextInt(counter) == 0) {
      result = root;
    }
    counter++;
    root = root.next;
  }
  return result;
}

//public class Solution {
   public List<Integer> inOrder(TreeNode root) {
     List<Integer> result = new ArrayList<>();
   Deque<TreeNode> stack = new ArrayDeque<>();

   while (root != null || !stack.isEmpty()) {
     if (root != null) {
       stack.offerLast(root);
       root = root.left;
     } else {
       root = stack.pollLast();
       result.add(root.key);
     root = root.right;
     }
    }
     return result;
}
}
//
//
public class Solution {
  public List<Integer> preOrder(TreeNode root) { 
     List<Integer> result = new ArrayList<>();
    Deque<TreeNode> stack = new ArrayDeque<>();
     while (root != null | ! stack.isEmpty()) {
       while (root != null) {
        result.add(root.key);
        stack.offerLast(root);
       root = root.left;
       }
         if (!stack.isEmpty()) {
         root = stack.pollLast();
           root = root.right;
         }
        
     }
    return result;
  }
}

//Q2
public void moveNeg(int[] arr) {
  //TODO: Sanity check
  int n = arr.length;
  int slow = 0;
  int fast = 0;
  while (fast < n) {
    if (arr[fast] > 0){
      swap(arr, fast++, slow++);
    } else {
      fast++;
    }
  }
  return;
} 
//Q3
public class MaxTree{
  pubblic int calMax(TreeNode root) {
    int[] cur = helper(root);
    return Math.max(cur[0], cur[1]);
  }
  
  private int[] helper(TreeNode root) {
    //max val int[0]: cur root chosen
    //max val int[1]: cur root not chosen
    if (root == null) {
      return new int[2] {0, 0};
    }
    
    int[] left = helper(root.left);
    int[] right = helper(root.right);
    
    int[] cur = new int[2];
    cur[0] = root.val + Math.max(left[1] + right[1]);
    cur[1] = Math.max(left[0], left[1]) + Math.max(right[0] + right[1]);
  }
}
//Q4
public class shuffleCard{
  public List<List<Integer>> shuffle(int n) {
    List<List<Integer>> results = new ArrayList<>();
    Set<Integer> used = new HashSet<>();
    int[] oneSol = new int[2 * n];
    dfs(0, n, used, oneSol, results);
  }
  private void dfs (int level, int n, Set<Integer> used, int[] oneSol, List<List<Integer>> results) {
    if (level == 2 * n) {
      if (used.size() == n) {
        results.add(new ArrayList<>(oneSol));
      }
      return;
    }
    if (oneSol[level] == 0) {
      for (int i = 1; i <= n; i++) {
        if (!used.contains(i) && level + i + 1 < n * 2) {
          oneSol[level] = i;
          oneSol[level + i + 1] = i;
          used.add(i);
          dfs(level + 1, n, used, oneSol, results);
          oneSol[level] = 0;
          oneSol[level + i + 1] = 0;
          used.remove(i);
        }
    }
    }
    
  }
}
//Q5
public class Solution {
   public List<Integer> postOrder(TreeNode root) {
    TreeNode cur = root;
    TreeNode pre = null;
    Deque<TreeNode> stack = new ArrayDeque<>();
    List<Integer> result = new ArrayList<>();
    while (cur != null || !stack.isEmpty()) {
      if (cur != null) {
        stack.offerLast(cur);
        pre = cur;
        cur = cur.left;
      } else {
        cur = stack.peekLast();
        if (cur.right != null && pre != cur.right) {
          //why check right != null? 希望pre全程不為null
           pre = cur;
           cur = cur.right;  
        } else { //pre == cur.right
           
          cur = stack.pollLast();
          result.add(cur.key);
          pre = cur;
          cur = null; 
        }
       
      }
    }
    return result;
  }
}



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子叠蝇,更是在濱河造成了極大的恐慌黔酥,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件形葬,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機啥辨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盯腌,“玉大人溉知,你說我怎么就攤上這事⊥蠊唬” “怎么了级乍?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長帚湘。 經(jīng)常有香客問我玫荣,道長,這世上最難降的妖魔是什么大诸? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任捅厂,我火速辦了婚禮,結(jié)果婚禮上资柔,老公的妹妹穿的比我還像新娘焙贷。我一直安慰自己,他們只是感情好贿堰,可當我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布辙芍。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪故硅。 梳的紋絲不亂的頭發(fā)上庶灿,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機與錄音吃衅,去河邊找鬼往踢。 笑死,一個胖子當著我的面吹牛捐晶,可吹牛的內(nèi)容都是我干的菲语。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼惑灵,長吁一口氣:“原來是場噩夢啊……” “哼山上!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起英支,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤佩憾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后干花,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體妄帘,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年池凄,在試婚紗的時候發(fā)現(xiàn)自己被綠了抡驼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡肿仑,死狀恐怖致盟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情尤慰,我是刑警寧澤馏锡,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站伟端,受9級特大地震影響杯道,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜责蝠,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一党巾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧霜医,春花似錦昧港、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽达舒。三九已至值朋,卻和暖如春叹侄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背昨登。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工趾代, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人丰辣。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓撒强,卻偏偏與公主長得像,于是被迫代替她去往敵國和親笙什。 傳聞我的和親對象是個殘疾皇子飘哨,可洞房花燭夜當晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,748評論 0 33
  • 聲音是頻率琐凭,音樂是頻率芽隆,宇宙 ——大無外,小無內(nèi)统屈,充滿了能量和信息胚吁,宇宙、銀河系愁憔、太陽系腕扶、地球、人吨掌、夸克都是有形狀...
    大威德金剛閱讀 292評論 0 0
  • 1.數(shù)組 1.1 數(shù)組構(gòu)造1.直接創(chuàng)建的方式 --> var arr = [1,2,3];2.構(gòu)造函數(shù)方式 -->...
    無聊的凡人閱讀 276評論 0 0
  • 題量有點多半抱,建議Ctrl + F題號或題目哦~ 二叉樹的遍歷(前序遍歷,中序遍歷思犁,后序遍歷)[144] Binar...
    野狗子嗷嗷嗷閱讀 9,103評論 2 37
  • 我喜歡的你代虾, 今天你身體好嗎? 還想以前一樣常喝酒熬夜嗎激蹲? 我喜歡的你棉磨, 今天你心情好嗎? 還想過往一樣愛唱歌跳舞...
    秋淡閱讀 129評論 0 0