some functions implemented from leetcode