Java solution for leetcode