精華區beta Marginalman 關於我們 聯絡資訊
※ 引述《smart0eddie (smart0eddie)》之銘言: : 1791. Find Center of Star Graph : There is an undirected star graph consisting of n nodes labeled from 1 to n. : A star graph is a graph where there is one center node and exactly n - 1 : edges that connect the center node with every other node. : 原本想說這題很簡單啊 : 所有node算一下誰的edge最多 : 然後時間跟別人比慢的要死 : 一看答案 : 靠北 直接取兩個edge 共通的node就好了 : 泥板只剩我不看答案連easy都不會了 思路: 哈希表紀錄node出現次數 出現兩次就是交點 Python Code: class Solution: def findCenter(self, edges: List[List[int]]) -> int: record = defaultdict(int) for i in range(len(edges)): record[edges[i][0]] += 1 record[edges[i][1]] += 1 if record[edges[i][0]] == 2: return edges[i][0] if record[edges[i][1]] == 2: return edges[i][1] 我本來想說要建圖 後來想想找到出現兩次的就好了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.43.138.195 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1719456839.A.D30.html