Leetcode Java solution