Leetcode solution in C++