Leetcode-997 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時,所求問題有解,返回節點編號。當滿足以上兩個條件的節點數量為0或大于1時,所求問題無解,返回值-1。
?
對于輸入樣例
N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]有圖 進而有表
? ? ? ? ? ??
?
1 #define pb push_back 2 #define maxSize 3939 3 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 4 5 class Solution 6 { 7 public: 8 int findJudge(int N, vector<vector<int>>& trust) 9 { 10 int hash[N+1][2]; 11 memset(hash,0,sizeof(hash)); 12 13 int sz = trust.size(); 14 _for(i,0,sz) 15 { 16 hash[trust[i][0]][0] ++; 17 hash[trust[i][1]][1] ++; 18 } 19 20 int rnt = -1; 21 _for(i,1,N+1) 22 { 23 if(hash[i][0]==0&&hash[i][1]==N-1) 24 { 25 if(rnt==-1) 26 rnt = i; 27 else 28 return -1; 29 } 30 } 31 return rnt; 32 } 33 }; Leetcode-997(C++)執行用時:364ms
轉載于:https://www.cnblogs.com/Asurudo/p/10427849.html
總結
以上是生活随笔為你收集整理的Leetcode-997 Find the Town Judge(找到小镇的法官)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用python中的xlrd和xlwt操
- 下一篇: (错误记录)git push 报错 40