Algorithms


BST Binary Search Tree Sep 20, 2017

Feature: In-order traverse --> sorted array

XOR Sep 20, 2017

Feature: a ^ b ^ b = a

Palindrome Sep 20, 2017

String Operations Sep 20, 2017

Remember regex operation.

Delete Duplicates in one scan Sep 20, 2017

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

Linked List-add node Sep 20, 2017

Zigzag String Sep 20, 2017

// Simulate is fine.
    a   e
     b d f
      c   g

Int overflow Sep 20, 2017

Continuously Get min/max Sep 20, 2017

String to Int Sep 20, 2017

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


3 Sums Sep 20, 2017

Find n to the end Sep 20, 2017

Blank Sep 20, 2017


Two Binary Search(1) Sep 21, 2017

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

Two Binary Search(2) Sep 21, 2017

//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)

Recursive Problem Sep 23, 2017

Long Number Operation Sep 25, 2017

Read More...

Backtracking Oct 6, 2017

Total Unique Paths -- DP Oct 14, 2017

Min Edit Distance -- DP Oct 18, 2017


Interleaving String -- DP Sep 17, 2017

Construct Binary Tree from Preorder and Inorder Traversal Sep 17, 2017

Path Sum II Sep 17, 2017


Tree Recursive Problems Sep 19, 2017

Consecutive Sequence Sep 19, 2017

HashMap put return value Sep 20, 2017


Iterate HashMap Sep 20, 2017


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

Tree Leaves Jan 3, 2018

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

BST + Lowest Common Ancestor Jan 3, 2018

// 只要p和q在一个subtree里,就说明不是lowest,直到第一个让它们俩不在一个subtree的时候
// Attention: This is a Binary Search Tree

Tree Height Jan 8, 2018

// Base case return -1
int treeHeight(TreeNode root) {
    if(root == null)    return -1;
    return Math.max(treeHeight(root.left), treeHeight(root.right)) + 1;
}

Traverse Tree Iteratively Jan 9, 2018

Count Set Bits of IntegerJan 18, 2018

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

Topological Sort Jan 27, 2018

DFS Template Jan 31, 2018

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

Majority Mar 10, 2018


Check Parentheses August 7, 2018

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

Back
Home
Top