leetcode997 Find the Town Judge

 1 """
 2 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.
 3 If the town judge exists, then:
 4     The town judge trusts nobody.
 5     Everybody (except for the town judge) trusts the town judge.
 6     There is exactly one person that satisfies properties 1 and 2.
 7 You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
 8 If the town judge exists and can be identified, return the label of the town judge.  Otherwise, return -1.
 9 Example 1:
10 Input: N = 2, trust = [[1,2]]
11 Output: 2
12 Example 2:
13 Input: N = 3, trust = [[1,3],[2,3]]
14 Output: 3
15 Example 3:
16 Input: N = 3, trust = [[1,3],[2,3],[3,1]]
17 Output: -1
18 Example 4:
19 Input: N = 3, trust = [[1,2],[2,3]]
20 Output: -1
21 Example 5:
22 Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
23 Output: 3
24 """
25 class Solution:
26     def findJudge(self, N, trust):
27         degree = [0]*(N+1)    #(N+1)的原因是要存对应1~N个人的度数
28         for i, j in trust:    #对每个trust对进行遍历
29             degree[i] -= 1    #第i个人出度减1
30             degree[j] += 1    #第j个人入度加1
31         for i in range(1, N+1):  #对1到N+1个人进行遍历
32             if degree[i] == N - 1: #找到出度为0,入度为N-1的人,即为town judge
33                 return i
34         return -1
35
36 """
37 开一个N+1长度的容器。
38 容器的索引是人的序号。
39 把对放入容器里。pair<出度,入度>。
40 遍历数组,更改每个人的出度和入度。
41 最后判断。出度为0,入度 为N-1的就是法官。
42 """
43
44
45 #自练:
46 #针对图问题,将向量转变成二维矩阵的方法
47 import numpy as np
48 array = np.ones((4, 4))*0
49 N = 4
50 check = [[0 for i in range(N)] for j in range(N)]
51 trust = [[1, 3], [1, 4], [2, 3], [2, 4], [4, 3]]
52 for pair in trust:
53     array[pair[0] - 1][pair[1] - 1] = 1
54     check[pair[0] - 1][pair[1] - 1] = 1
55 print(array)
56 print(check)

原文地址:https://www.cnblogs.com/yawenw/p/12257553.html

时间: 2024-10-18 04:51:11

leetcode997 Find the Town Judge的相关文章

Leetcode-997 Find the Town Judge(找到小镇的法官)

作者水平有限,所发仅为个人愚见,如有明显谬误,望斧正 题目可转化为对于所给正整数N(1≤N≤1000),共有N个节点,编号从1-N.其中"相信"这一概念,可看作是一条连接两节点的有向边.如所给二维vector的trust向量数组,trust[i][0]表示有向边的起点,则trust[i][1]表示有向边的终点.所求为满足以下2个条件的节点:①出度(即题目中的属性1)为0 ②入度(即题目中的属性2)为N-1.当且仅当满足以上两个条件的节点数量为1时,所求问题有解,返回节点编号.当满足以上

#Leetcode# 997. Find the Town Judge

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: The town judge trusts nobody. Everybody (exc

【Leetcode_easy】997. Find the Town Judge

problem 997. Find the Town Judge 参考 1. Leetcode_easy_997. Find the Town Judge; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11316973.html

leetcode 997. 找到小镇的法官(Find the Town Judge)

目录 题目描述: 示例 1: 示例 2: 示例 3: 示例 4: 示例 5: 解法: 题目描述: 在一个小镇里,按从 1 到 N 标记了 N 个人.传言称,这些人中有一个是小镇上的秘密法官. 如果小镇的法官真的存在,那么: 小镇的法官不相信任何人. 每个人(除了小镇法官外)都信任小镇的法官. 只有一个人同时满足属性 1 和属性 2 . 给定数组 trust,该数组由信任对 trust[i] = [a, b] 组成,表示标记为 a 的人信任标记为 b 的人. 如果小镇存在秘密法官并且可以确定他的身

LeetCode.997-找到镇法官(Find the Town Judge)

这是悦乐书的第373次更新,第400篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第234题(顺位题号是997).在一个城镇,有N个人从1到N标记.有传言说其中一个人是秘密的镇法官. 如果镇法官存在,那么: 镇法官不信任任何人. 每个人(镇法官除外)都信任镇法官. 只有一个人满足前两条. 给定一个trust数组,一对trust[i] = [a,b]表示被标记为a的人信任标记为b的人. 如果镇法官存在并且可以识别,则返回镇法官的标签.否则,返回-1. 例如: 输入:N

谔勒寇人镁qphr1j7zw22va61

http://www.qiushibaike.com/tag/%e5%82%ac%e6%83%85%e7%b2%89%e4%b8%8a%e5%93%aa%e8%83%bd%e4%b9%b0%2b%ef%bd%91%ef%bc%92%ef%bc%98%ef%bc%95%ef%bc%98%ef%bc%92%ef%bc%99%ef%bc%91%ef%bc%92%ef%bc%90.http://www.gxxc.gov.cn/Town/TownDetails?id=94210&town=%e5%90%8

最小生成树-并查集-Kruskal-zoj-2048-special judge

Highways description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting som

657. Judge Route Circle 判断线路成圆

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is represented by a string. And each move is represent by a characte

Special Judge Ⅱ

Problem Description Q:什么是 Special Judge,Special Judge 的题目有什么不同? A:一个题目可以接受多种正确答案,即有多组解的时候,题目就必须被 Special Judge.Special Judge 程序使用输入数据和一些其他信息来判答程序的输出,并将判答结果返回. NaYe 最近遇到了一个题,要求输出三个数,第三个数为前两个数的和,三个数都是素数,且前两个数小于 500000.他只需要输出任意一组符合要求的答案即认为是 Accepted.现在需