首页 > 其他分享 >LeetCode 399. 除法求值

LeetCode 399. 除法求值

时间:2023-07-24 12:33:20浏览次数:31  
标签:return int LeetCode vector 求值 399 st true unordered

class Solution {
public:
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        unordered_set<string> node;//记录所有节点
        unordered_map<string,unordered_map<string,double>> g;//存储图
        int n=equations.size(),m=queries.size();
        for(int i=0;i<n;i++)
        {
            string a=equations[i][0],b=equations[i][1];
            double c=values[i];
            //a->b的权值为c
            node.insert(a);node.insert(b);
            g[a][b]=c;g[b][a]=1/c;
        }
        vector<double> res;
        for(int i=0;i<m;i++)//处理询问
        {
            string a=queries[i][0],b=queries[i][1];
            if(node.find(a)==node.end()||node.find(b)==node.end())
                res.push_back(-1);
            else
            {
                double path=1;
                unordered_map<string,bool> st;
                st[a]=true;
                if(!dfs(a,b,g,path,st))   path=-1;
                res.push_back(path);
           }
        }
        return res;
    }
    bool dfs(string a,string b,unordered_map<string,unordered_map<string,double>> &g,double &sum,unordered_map<string,bool> &st)
    {
        if(a==b)    
            return true;
        for(auto item:g[a])
        {
            string t=item.first;
            if(!st[t])
            {
                st[t]=true;
                if(dfs(t,b,g,sum,st))
                {
                    sum*=item.second;
                    return true;
                }
            }
        }
        return false;
    }
};

标签:return,int,LeetCode,vector,求值,399,st,true,unordered
From: https://www.cnblogs.com/tangxibomb/p/17576932.html

相关文章

  • leetcode第109场双周赛
    题目传送门6931.访问数组中的位置使分数最大题意给你一个数组,初始你位于下标1处,你可以往后跳到数组任一下标,但不能往前跳。跳到哪个位置,即可获得下标对应的分数,但如果当前下标的数与跳之前下标的数奇偶性不同,那么你会失去分数x。询问你能获得的最大分数?思路一眼动态规划,......
  • LeetCode 周赛上分之旅 #35 两题坐牢,菜鸡现出原形
    ⭐️本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]和[BaguTreePro]知识星球提问。学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场LeetCode周赛的解题报告......
  • leetcode第 109 场双周赛
    6930.检查数组是否是好的-力扣(LeetCode)首先判断数组长度是不是最大值+1,然后排个序,判断0到n-2是不是都是1到最大值的一个排列,满足这些返回true就行了classSolution{public:boolisGood(vector<int>&num){intma=0;for(autoi:num){......
  • Rockchip RK3399 - 音频调试
    一、内核配置1.1配置内核修改sound/soc/codecs/Kconfig文件,搜索configSND_SOC_RT5651,将该项修configSND_SOC_RT5651tristatedependsonI2C改为,如果不修改的话,makemenuconfig是看不到该配置项的;configSND_SOC_RT5651tristate"RealtekAL......
  • java实现leetcode 22 括号的生成
    Java实现Leetcode22-括号的生成本文将介绍如何使用Java实现Leetcode22题-括号的生成,并提供相应的代码示例。括号的生成是一个经典的递归问题,通过理解递归的思想和括号生成的规律,我们可以很容易地解决这个问题。题目描述给定一个整数n,表示生成括号的对数,编写一个函数来生成......
  • 【优先队列】【堆排序实现优先队列】[1054. 距离相等的条形码](https://leetcode.cn/p
    【优先队列】【堆排序实现优先队列】1054.距离相等的条形码在一个仓库里,有一排条形码,其中第i个条形码为barcodes[i]。请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。你可以返回任何满足该要求的答案,此题保证存在答案。示例1:输入:barcodes=[1,1,1,2,2,2]......
  • [LeetCode] 894. All Possible Full Binary Trees
    Givenaninteger n,return alistofallpossible fullbinarytrees with n nodes.Eachnodeofeachtreeintheanswermusthave Node.val==0.Eachelementoftheansweristherootnodeofonepossibletree.Youmayreturnthefinallistoftreesin......
  • leetcode-2582-easy
    PassthePillowTherearenpeoplestandinginalinelabeledfrom1ton.Thefirstpersoninthelineisholdingapillowinitially.Everysecond,thepersonholdingthepillowpassesittothenextpersonstandingintheline.Oncethepillowreachest......
  • Leetcode394. 字符串解码
    classSolution{public:stringdfs(strings,int&idx){stringstr;while(idx<s.size()){if(s[idx]==']'){idx++;returnstr;}......
  • leetcode 栈与队列 232 225
    目录基本介绍四个问题232225基本介绍栈,先进后出队列,先进先出四个问题C++中stack是容器么?我们使用的stack是属于哪个版本的STL?我们使用的STL中stack是如何实现的?stack提供迭代器来遍历stack空间么?首先大家要知道栈和队列是STL(C++标准库)里面的两个数据结构。C++标准......