Intersection complexity

前端 未结 3 1244
小蘑菇
小蘑菇 2020-12-09 10:33

In Python you can get the intersection of two sets doing:

>>> s1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> s2 = {0, 3, 5, 6, 10}
>>> s1 &a         


        
3条回答
  •  甜味超标
    2020-12-09 11:00

    The answer appears to be a search engine query away. You can also use this direct link to the Time Complexity page at python.org. Quick summary:

    Average:     O(min(len(s), len(t))
    Worst case:  O(len(s) * len(t))
    

    EDIT: As Raymond points out below, the "worst case" scenario isn't likely to occur. I included it originally to be thorough, and I'm leaving it to provide context for the discussion below, but I think Raymond's right.

提交回复
热议问题