Leetcode Problem 2 Solution In Java