2023-08-29力扣每日一题

链接:

823. 带因子的二叉树

题意:

用给的数字建二叉树,要求父节点是子节点的乘积

解:

乐了

1500ms+30MB //注释版120ms+18MB

实际代码:

#include
using namespace std;
static constexpr int Mod=1E9+7;
int numFactoredBinaryTrees(vector& arr)
{
    mapmp;
    for(auto ar:arr) mp[ar]=1;

    //setst(arr.begin(),arr.end());
    //int lg=st.size();
    long long ret=0;
    mapdp;

    //for(auto s1:st)
    for(auto mp1:mp)
    {
        dp[mp1.first]=1;
        for(auto mp2:mp)
        {
            if(mp2.first==mp1.first) break;
            if(mp1.first%mp2.first==0 && mp[mp1.first/mp2.first]>0)
            {
                //cout<& arr)
{
    mapmp;
    for(auto ar:arr) mp[ar]=1;

    setst(arr.begin(),arr.end());
    int lg=st.size();
    long long ret=0;
    mapdp;
    
    for(auto s1:st)
    {
        dp[s1]=1;
        for(auto s2:st)
        {
            if(s2==s1) break;
            if(s1%s2==0 && mp[s1/s2]>0)
            {
                //cout<in;int temp;
    while(cin>>temp)in.push_back(temp);
    int ans=numFactoredBinaryTrees(in);
    cout<

限制:

  • 1 <= arr.length <= 1000
  • 2 <= arr[i] <= 109
  • arr 中的所有值 互不相同

你可能感兴趣的:(力扣每日一题,leetcode)