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 inOrder(TreeNode root) {
     List result = new ArrayList<>();
   Deque 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 preOrder(TreeNode root) { 
     List result = new ArrayList<>();
    Deque 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> shuffle(int n) {
    List> results = new ArrayList<>();
    Set used = new HashSet<>();
    int[] oneSol = new int[2 * n];
    dfs(0, n, used, oneSol, results);
  }
  private void dfs (int level, int n, Set used, int[] oneSol, List> 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 postOrder(TreeNode root) {
    TreeNode cur = root;
    TreeNode pre = null;
    Deque stack = new ArrayDeque<>();
    List 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;
  }
}



你可能感兴趣的:(2018-07-02)