Leetcode开心刷题三十九——94递归大水题,速度慢95. Unique Binary Search Trees II99没看会神级解法Morris算法73通过或位运算简化代码,但未实现最简便的方法常数时间

 
Medium
1453 123 Favorite Share

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.

Example:

Input: 3
Output:
[
  [1,null,3,2],
  [3,2,null,1],
  [3,1,null,null,2],
  [2,1,3],
  [1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

ATTENTION POINT
defination of tree
#include 
#include
using namespace std;
#include 
#include
//segement default内存操作不当
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x):val(x),left(NULL),right(NULL) {}
};
class Solution
{
public:
    vector generateTrees(int n)
    {
        if(n==0) return {};
        const auto& ans=generateTrees(1,n);
        cout< generateTrees(int l,int r)
    {
        if(l>r) return {nullptr};
        vector ans;
        for(int i=l;i<=r;++i)
            for(TreeNode* left:generateTrees(l,i-1))
                for(TreeNode* right:generateTrees(i+1,r))
                {
                   ans.push_back(new TreeNode(i));
                   ans.back()->left=left;
                   ans.back()->right=right;
                }
        return ans;

    }

};
//class Solution {
//public:
//  vector generateTrees(int n) {
//    if (n == 0) return {};
//    const auto& ans = generateTrees(1, n);
//    cout << ans.size() << endl;
//    return ans;
//  }
//private:
//  vector generateTrees(int l, int r) {
//    if (l > r) return { nullptr };
//    vector ans;
//    for (int i = l; i <= r; ++i)
//      for (TreeNode* left : generateTrees(l, i - 1))
//        for (TreeNode* right : generateTrees(i + 1, r)) {
//          ans.push_back(new TreeNode(i));
//          ans.back()->left = left;
//          ans.back()->right = right;
//        }
//    return ans;
//  }
//};
void printTree(TreeNode* tmp)
{
    if(tmp==nullptr)
        return;
    else{
        cout<val;
        printTree(tmp->left);
        printTree(tmp->right);
    }

}
int main()
{
    Solution s1;
    int n=3;
//    cin>>n;
    vector ans=s1.generateTrees(n);
    for(auto i:ans)
    {
        printTree(i);
        cout< 
  

  

LeetCode73
#include 
#include
#include
using namespace std;
class Solution {
public:
    void setZeroes(vectorint>>& matrix)
    {
        int m=matrix.size();
        int n=matrix[0].size();
        vector<int> rows(m);
        vector<int> cols(n);

        for(int i=0;i)
            for(int j=0;j){
                rows[i]|=(matrix[i][j]==0);
                cols[j]|=(matrix[i][j]==0);
            }

        for(int i=0;i)
            for(int j=0;j){
                if(cols[j]||rows[i]) matrix[i][j]=0;
            }

    }

};
int main()
{
    Solution s1;
    vectorint>> matrix=
    {
        {1,1,1},
        {1,0,1},
        {1,1,1}
    };
    s1.setZeroes(matrix);
    for(int i=0;i){
        for(int j=0;j0].size();j++){
                cout<<matrix[i][j];
        }
        cout<<endl;
    }


    return 0;

}
 
  

 

 

你可能感兴趣的:(Leetcode开心刷题三十九——94递归大水题,速度慢95. Unique Binary Search Trees II99没看会神级解法Morris算法73通过或位运算简化代码,但未实现最简便的方法常数时间)