LeetCode solution in java