Solution to leetcode.