Kotlin Leetcode - 1791. Find Center of Star Graph
class Solution {
fun findCenter(edges: Array<IntArray>): Int {
}
}
解題思路
這一題乍看之下很難,要處理圖的邏輯
其實由於題目的性質
你只需要處理兩個 edge
就可以得知哪個 node 是 center
性質更接近於陣列運算
Kotlin 參考解答
點擊展開解答
單一表達式內完成的方式如下
class Solution {
fun findCenter(edges: Array<IntArray>) =
if (edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1])
edges[0][0] else edges[0][1]
}
回到 leetcode 列表