Java solution for Leetcode