forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db9b9a3
commit 62bcfca
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(cpp_0096) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main2.cpp) | ||
add_executable(cpp_0096 ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/// Source : https://leetcode.com/problems/unique-binary-search-trees/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-10 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Memory Search | ||
/// Time Complexity: O(n) | ||
/// Space Complexity: O(n) | ||
class Solution { | ||
|
||
public: | ||
int numTrees(int n) { | ||
|
||
vector<int> dp(n + 1, -1); | ||
return numTrees(n, dp); | ||
} | ||
|
||
private: | ||
int numTrees(int n, vector<int>& dp){ | ||
|
||
if(n <= 1) | ||
return 1; | ||
|
||
if(dp[n] != -1) | ||
return dp[n]; | ||
|
||
int res = 0; | ||
for(int i = 1; i <= n; i ++) | ||
res += numTrees(i - 1, dp) * numTrees(n - i, dp); | ||
return dp[n] = res; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
cout << Solution().numTrees(3) << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/// Source : https://leetcode.com/problems/unique-binary-search-trees/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-10 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Dynamic Programming | ||
/// Time Complexity: O(n) | ||
/// Space Complexity: O(n) | ||
class Solution { | ||
|
||
public: | ||
int numTrees(int n) { | ||
|
||
vector<int> dp(n + 1, 0); | ||
dp[0] = dp[1] = 1; | ||
for(int i = 2; i <= n; i ++) | ||
for(int j = 1; j <= i; j ++) | ||
dp[i] += dp[j - 1] * dp[i - j]; | ||
return dp[n]; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
cout << Solution().numTrees(3) << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,8 @@ email: [[email protected]](mailto:[email protected]) | |
| | | | | | | | ||
| 094 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) | [solution](https://leetcode.com/problems/binary-tree-inorder-traversal/solution/) | [C++](0094-Binary-Tree-Inorder-Traversal/cpp-0094/) | [Java](0094-Binary-Tree-Inorder-Traversal/java-0094/src/) | | | ||
| | | | | | | | ||
| 096 | [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/description/) | [无] | [C++](0096-Unique-Binary-Search-Trees/cpp-0096/) | | | | ||
| | | | | | | | ||
| 098 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/) | [无] | [C++](0098-Validate-Binary-Search-Tree/cpp-0098/) | [Java](0098-Validate-Binary-Search-Tree/java-0098/src/) | | | ||
| 099 | [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/description/) | [无] | | [Java](0099-Recover-Binary-Search-Tree/java-0099/src/) | | | ||
| | | | | | | | ||
|