Accepted code for LeetCode