LeetCode – Verify Preorder Serialization of a Binary Tree (Java)
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. 9 / \...
View ArticleHow to determine if a string is English or Java code?
Consider the following two strings: 1. for (int i = 0; i < b.size(); i++) { 2. do something in English (not necessary to be a sentence). The first one is Java code, the second one is English. How to...
View ArticleLeetCode – Find Median from Data Stream (Java)
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Analysis First of all, it seems that...
View ArticleLeetCode – Number of Islands II (Java)
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate,...
View ArticleJava Remove Element from ArrayList
To remove some elements from an ArrayList while iterating over the ArrayList, we need to use Iterator. Integer[] arr = {1,2,3,4,5,6}; ArrayList list = new ArrayList(Arrays.asList(arr));...
View ArticleLeetCode – Patching Array (Java)
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array....
View Article