diff options
author | crupest <crupest@outlook.com> | 2020-07-24 16:10:08 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-07-24 16:10:08 +0800 |
commit | 578f3da6699e07e2668ba74dc0caf0a16d293e37 (patch) | |
tree | deb50fce55a3f12e0f72db077c100f3363a10d31 /works/solutions/cpp/96.cpp | |
parent | e703e4c1fcef4d94a3c5fa2ef2dba4c6b5eb7591 (diff) | |
parent | 1b5884ab3905fc3569d7b48e077a0b3a9ae24f67 (diff) | |
download | crupest-578f3da6699e07e2668ba74dc0caf0a16d293e37.tar.gz crupest-578f3da6699e07e2668ba74dc0caf0a16d293e37.tar.bz2 crupest-578f3da6699e07e2668ba74dc0caf0a16d293e37.zip |
import(solutions): Merge branch 'master' of https://github.com/crupest/leetcode
Diffstat (limited to 'works/solutions/cpp/96.cpp')
-rw-r--r-- | works/solutions/cpp/96.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/works/solutions/cpp/96.cpp b/works/solutions/cpp/96.cpp new file mode 100644 index 0000000..a67a59a --- /dev/null +++ b/works/solutions/cpp/96.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + // catalan number: + // f(n) = f(0) * f(n-1) + f(1) * f(n-2) + ... + f(n-1)*f(0) + // f(n) = 2(2n-1) * f(n-1) / (n+1) + // f(n) = C(2n, n) / (n+1) + int numTrees(int n) { + long long result = 1; + for (int i = 2; i <= n; i++) { + result = 2 * (2 * i - 1) * result / (i + 1); + } + return result; + } +}; |