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;
  }
}



?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,768評論 0 33
  • 聲音是頻率,音樂是頻率,宇宙 ——大無外,小無內,充滿了能量和信息,宇宙、銀河系、太陽系、地球、人、夸克都是有形狀...
    大威德金剛閱讀 299評論 0 0
  • 1.數組 1.1 數組構造1.直接創建的方式 --> var arr = [1,2,3];2.構造函數方式 -->...
    無聊的凡人閱讀 279評論 0 0
  • 題量有點多,建議Ctrl + F題號或題目哦~ 二叉樹的遍歷(前序遍歷,中序遍歷,后序遍歷)[144] Binar...
    野狗子嗷嗷嗷閱讀 9,128評論 2 37
  • 我喜歡的你, 今天你身體好嗎? 還想以前一樣常喝酒熬夜嗎? 我喜歡的你, 今天你心情好嗎? 還想過往一樣愛唱歌跳舞...
    秋淡閱讀 134評論 0 0