Java solution for Leetcode problems