Accepted code for Leetcode.