LeetCode Java solution