LeetCode – Remove Duplicates from Sorted Array II (Java)

Follow up for “Remove Duplicates“: What if duplicates are allowed at most twice? For example, given sorted array A = [1,1,1,2,2,3], your function should return length = 5, and A is now [1,1,2,2,3]. So this problem also requires in-place array manipulation. Java Solution 1 We can not change the given array’s size, so we only … Read more

Can Constructor Throw Exceptions in Java?

1. The question In Java, methods can throw exceptions. Can constructors also throw exceptions? The answer is YES. 2. The reason and some necessary background A constructor is just a special method. In this perspective, it surely can do what regular methods can do. It is possible that there are some objects created and assigned … Read more

LeetCode – Convert Sorted Array to Binary Search Tree (Java)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Java Solution A typical DFS problem using recursion. // Definition for binary tree class TreeNode { int val; TreeNode left; TreeNode right;   TreeNode(int x) { val = x; } }   public class Solution { public TreeNode … Read more

LeetCode – Convert Sorted List to Binary Search Tree (Java)

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Thoughts If you are given an array, the problem is quite straightforward. But things get a little more complicated when you have a singly linked list instead of an array. Now you no longer have random … Read more

Sort LinkedList of User-Defined Objects in Java

For sorting a list in Java, you can use sort(List<T> list) method. This method can sort a list in which all elements must implement the Comparable interface. In the example below, the House class is user-defined. To make it comparable, it implements the Comparable interface. By using the sort(List<T> list) method, it can be sorted … Read more

LeetCode – Subsets (Java)

Given a set of distinct integers, S, return all possible subsets. Note: 1) Elements in a subset must be in non-descending order. 2) The solution set must not contain duplicate subsets. For example, given S = [1,2,3], the method returns: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Thoughts Given a set S … Read more

LeetCode – Valid Palindrome (Java)

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “Red rum, sir, is murder” is a palindrome, while “Programcreek is awesome” is not. Note: Have you consider that the string might be empty? This is a good question to ask during an interview. For the purpose … Read more

LeetCode – Longest Consecutive Sequence (Java)

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, given [100, 4, 200, 1, 3, 2], the longest consecutive elements sequence should be [1, 2, 3, 4]. Its length is 4. Your algorithm should run in O(n) complexity. Java Solution 1 Because it requires O(n) complexity, we … Read more