Feature: In-order traverse --> sorted array
if(root == null) return;
Feature: a ^ b ^ b = a
Remember regex operation.
//Copy non-dup to the front of the arr
int len = 0;
for(int i=0 ; i < nums.length ; i++)
{
if(val != nums[i])
{
nums[len] = nums[i];
len++;
}
}
//1, Create HeadNode
ListNode p = new ListNode(0);
ListNode head = p;
//2,Add
ListNode tempResult = new ListNode(0);
p.next = tempResult;
p = p.next;
//1, Create HeadNode
ListNode newHead = new ListNode(-1);
while(head != null)
{
ListNode temp = head.next;//tempNode
head.next = newHead.next;
newHead.next = head;
head = temp;
}
// Simulate is fine.
a e
b d f
c g
Calculate Sign, then pile them
//1,Sign
sign = 1 - 2 * (str[i++] == '-');
//2, Add(in and against the order)
base = 10 * base + (str[i++] - '0');//in order
//or
for(int i=str.length()-1 ; i>=0 ; i--)// against order
{
result += (str.charAt(i)-'0') * Math.pow(10,p);
p ++;
}
//1, Normal one
while(low<=high) // here "="
{
mid = (low + high)/2;
if(arr[mid] > x)
high = mid - 1;
else if(arr[mid] < x)
low = mid + 1;
else if(arr[mid] == x)
return mid;
}
return -1;
//2, Insert elements or find the first target
while(low <= high)
{
int mid = (low+high)/2;
if(nums[mid]>=target)
high = mid-1;
else
low = mid+1;
}
return low;
// eg: [1,2,2,2,3] target=2, result: 1 (leftest one found)
// [1,3,3,3,4] target=2, result: 1 (insert index)
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
// if mentioned leaves, must be root.left == null && root.right == null
// plus an operation on this node
if(root.left == null && root.right == null) { //leaf
step = step + "->" +root.val; //operation on this node
result.add(step.substring(2, step.length()));
return;
}
// 只要p和q在一个subtree里,就说明不是lowest,直到第一个让它们俩不在一个subtree的时候
// Attention: This is a Binary Search Tree
// Base case return -1
int treeHeight(TreeNode root) {
if(root == null) return -1;
return Math.max(treeHeight(root.left), treeHeight(root.right)) + 1;
}
// O(log(n))
int count_set_bits(int n){
int count = 0; // count accumulates the total bits set
while(n != 0){
n &= (n-1); // clear the least significant bit set
count++;
}
}
// DFS
boolean dfs(int[][] n, boolean[][] visited, int row, int col){
if(...)
return true;
if(row < 0 || col < 0 || row >= board.length || col >= board[0].length || visited[row][col])
return false;
if(condition) {
visited[row][col] = true; // make it visited
boolean found = dfs(n, visited, row + 1, col);
found |= dfs(n, visited, row - 1, col);
found |= dfs(n, visited, row, col + 1);
found |= dfs(n, visited, row, col - 1);
if(!found)
visited[row][col] = false; // if unsucessuful, turn visited back
}
}
// Count left ( then pair it with )
public boolean isValid(StringBuffer str) {
int count = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) == '(')
count ++;
else if(str.charAt(i) == ')')
count --;
if(count < 0)
return false;
}
return count == 0;
}