LeetCode – Matrix Zigzag Traversal

Java Solution public int[] printZMatrix(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) { return new int[0]; } int row = matrix.length; int col = matrix[0].length; int[] res = new int[row * col]; int lvl = 0; int next = 0; while (next = 0 … Continue reading LeetCode – Matrix Zigzag Traversal

Storing a two-integer element (coordinate, result set)?

I did a little test today about how to store a two-integer element like coordinate or global/local result set. The most compact way, which I prefer, is to use a long to store the two results. public static void main (String[] args) { long point = 0L; long lowMask = 0x00000000FFFFFFFF; int x = -7; int … Continue reading Storing a two-integer element (coordinate, result set)?

LeetCode – Palindrome Partitioning II

The first thing that comes up is that we should be able to solve this problem using DFS. For each recursion we do: 1.From designated start point, we scan from len - 1 down to start point to see if s.substring(i, j) is palindrome. 2.If so, we do dfs(s, j + 1), record the minimum … Continue reading LeetCode – Palindrome Partitioning II

LeetCode – Best Time to Buy and Sell Stock IV Answers/Solution/Thoughts

This is the generic version of following problems Best Time to Buy and Sell Stock III (two transactions) Best Time to Buy and Sell Stock II (unlimited transactions) Best Time to Buy and Sell Stock (one transaction) So there are two dimensions to be considered: the number of transactions and the profit up to a given point of … Continue reading LeetCode – Best Time to Buy and Sell Stock IV Answers/Solution/Thoughts