Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-06-27 10:53:56
※ 引述《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]
我本来想说要建图 后来想想找到出现两次的就好了

Links booklink

Contact Us: admin [ a t ] ucptt.com