The solution of LeetCode in Java