C++ code for leetcode OJ 151/151 with comments