C++ implementation for LeetCode problem