The fastest C++ code can achieve a time about 370ms ~ 380ms
while Python code can achieve 100ms, and even amazing, JAVA can do it in just 4ms !
This result is quite counterintuitive.
I think the C++ version needs to allocate every node in the system heap, which is a horribly time-consuming operation.
The Python and Java version probably have some kind of memory pool.
Java currently only measures the runtime in the user function execution excluding the JVM startup time plus the driver code execution time. C++ measures the entire program execution, so it is not meaningful to compare the runtime between languages.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.