https://leetcode.com/problems/find-the-town-judge/

In a town, there are N people labelled from 1 to N.  There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:

  1. The town judge trusts nobody.
  2. Everybody (except for the town judge) trusts the town judge.
  3. There is exactly one person that satisfies properties 1 and 2.

You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.

If the town judge exists and can be identified, return the label of the town judge.  Otherwise, return -1.

Example 1:

Input: N = 2, trust = [[1,2]]
Output: 2

Example 2:

Input: N = 3, trust = [[1,3],[2,3]]
Output: 3

Example 3:

Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1

Example 4:

Input: N = 3, trust = [[1,2],[2,3]]
Output: -1

Example 5:

Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
Output: 3

Note:

  1. 1 <= N <= 1000
  2. trust.length <= 10000
  3. trust[i] are all different
  4. trust[i][0] != trust[i][1]
  5. 1 <= trust[i][0], trust[i][1] <= N

代码:

class Solution {
public:
vector<int> v[1010];
int vis[1010];
int findJudge(int N, vector<vector<int>>& trust) {
int n = trust.size(); for(int i = 0; i < n; i ++) {
int a = trust[i][0], b = trust[i][1];
v[b].push_back(a);
vis[a] = 1;
} int cnt = 0, temp = 0;
for(int i = 1; i <= N; i ++) {
if(vis[i] == 0 && v[i].size() == N - 1) {
cnt ++;
temp = i;
}
}
if(cnt == 1) return temp;
return -1;
}
};

最新文章

  1. iOS开发小技巧 - label中的文字添加点击事件
  2. 将Java应用程序打包成可执行的Jar包
  3. Java-继承,多态练习09-22-01
  4. c++之vector
  5. JS鼠标滚动事件
  6. RMAN备份与恢复深入解&lt;一&gt;
  7. 转:栈和队列小知识【STL用法】
  8. javaScript 自定义事件、发布订阅设计模式
  9. javascript面向对象基础讲解(工厂模式、构造函数模式、原型模式、混合模式、动态原型模式)
  10. OSChina 的URL类的源代码重写过程
  11. HttpWebRequest BeginGetResponse EndGetResponse
  12. DDD Reference
  13. vue组件,撸第一个
  14. 使用 electron 做个播放器
  15. 使用Jquery.cookie.js操作cookie
  16. DB---数据库中Schema的理解
  17. 迷茫&lt;第三篇:再到北京&gt;
  18. 使用@Autowired时,取值为null
  19. 【代码笔记】Web-JavaScript-javascript while循环
  20. json&amp;pickle序列化和软件开发规范

热门文章

  1. 百度图片objURL解密vb.net版
  2. Java开发笔记(四十六)类的构造方法
  3. 修改tomcat的端口号
  4. PHP基础:MYSQL数据库操作
  5. Asp.Net MVC @Html.TextBox 只允许输入数字问题
  6. 广州.NET微软技术俱乐部微信群各位技术大牛的blog
  7. BGP:我们不生产路由,而是路由的搬运工
  8. Scrapped or attached views may not be recycled
  9. Android 异步框架 RxJava2
  10. C#实现完整的防盗自制监控系统