aboutsummaryrefslogtreecommitdiff
path: root/works/solutions/cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-07-22 20:23:08 +0800
committerGitHub <noreply@github.com>2020-07-22 20:23:08 +0800
commit1b5884ab3905fc3569d7b48e077a0b3a9ae24f67 (patch)
tree3e5722f0c7fda49f81ee98b33eb316f1f3b594a0 /works/solutions/cpp
parent57e3b304a4d4e9f16f53ffc8d65ecb823b047645 (diff)
downloadcrupest-1b5884ab3905fc3569d7b48e077a0b3a9ae24f67.tar.gz
crupest-1b5884ab3905fc3569d7b48e077a0b3a9ae24f67.tar.bz2
crupest-1b5884ab3905fc3569d7b48e077a0b3a9ae24f67.zip
import(solutions): Add problem 96 .
Diffstat (limited to 'works/solutions/cpp')
-rw-r--r--works/solutions/cpp/96.cpp14
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;
+ }
+};