Accepted Code for Leetcode