【Lintcode】python - [793] Intersection of Arrays 個人解法筆記

題目出處

793 · Intersection of Arrays

難度

medium

個人範例程式碼

from typing import (
    List,
)

class Solution:
    """
    @param arrs: the arrays
    @return: the number of the intersection of the arrays
    """
    def intersection_of_arrays(self, arrs: List[List[int]]) -> int:
        # write your code here
        ans = set(arrs[0])
        for arr in arrs:
            ans &= set(arr)

        return len(ans)

最近在練習程式碼本身就可以自解釋的 Coding style,可以嘗試直接閱讀程式碼理解

算法說明

題目說不需要重複,那就是瘋狂的 set and 取交集。

input handling

set and 的過程會幫我們處理掉

Boundary conditions

x

Reference