Given:
[(1,2),(3,4),(5,6),(3,7),(5,7)]
Output:
[set(1,2), set(3,4,5,6,7)]
解释:
(1,2)
(1,2), (3,4)
(1,2), (3,4), (5,6)
(1,2), (3,4,7), (5,6)
(1,2), (3,4,7,5,6)
我写了一个糟糕的算法:
Case 1: both numbers in pair are new (never seen before):
Make a new set with these two numbers
Case 2: one of the number in pair is new, other is already a part of some set:
Merge the new number in other's set
Case 3: both the numbers belong to some set:
Union the second set into first. Destroy the second set.
这个算法有没有更Pythonic(奇特)的解决方案?