LeetCode题解之 Find the Town Judge

1、题目描述

2、问题分析

使用map set数据结构。

3、代码

 1  int findJudge(int N, vector<vector<int>>& trust) {
 2         
 3         if (trust.size() == 0)
 4             return 1;
 5         
 6         int n = 0;
 7         map<int, int> m;
 8         set<int> s;
 9         for (auto it = trust.begin(); it != trust.end(); it++) {
10             m[(*it).back()]++;
11             s.insert((*it).front());
12         }
13         
14         int peo = INT_MIN;
15         for (auto it = m.begin(); it != m.end(); it++) {
16             if (it->second > n){
17                 n = it->second;
18                 peo = it->first;
19             }
20         }
21         
22         return ((n == (N - 1)) && s.find(peo) == s.end()) ? peo : -1;
23         
24     }

猜你喜欢

转载自www.cnblogs.com/wangxiaoyong/p/10462999.html