Java solution for leetcode online judge