Re: [閒聊] 每日leetcode

作者: JIWP (JIWP)   2024-06-27 23:43:48
1791. Find Center of Star Graph
有一個星型的圖,包含1~n個節點
星型圖就是只有一個中心,並且有n-1條邊
請回傳中心節點
思路:
看哪一個點出現超過2次
就是中心
golang code :
func findCenter(edges [][]int) int {
rec:=make([]int,len(edges)+1)
for i:=range edges{
rec[edges[i][0]-1]++
rec[edges[i][1]-1]++
}
for i:=range rec{
if rec[i]==len(edges){
return i+1
}
}
return 0
}
作者: SecondRun (雨夜琴聲)   2024-06-27 23:46:00
大師
作者: sustainer123 (caster)   2024-06-27 23:54:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com