Accepted code for Leetcode