# -*- coding:utf-8 -*- from collections import Counter class Solution(object): def findRepeatNum(self,input_list): if not input_list: return c = Counter(input_list) result = set() for index,num in enumerate(input_list): if c[num] >= 2: result.add(num) return result def find2(self,input_list): if not input_list: return #step 1 : sorted input_list.sort() result = set() # for index in range(len(input_list)-1): if input_list[index] == input_list[index+1]: result.add(input_list[index]) return result #step 2:iter def find3(self,input_list): ##check input if not input_list: return for num in input_list: if num <0 or num > (len(input_list)-1): return res = set() for index in range(0,len(input_list)): #如果index代表的元素,如果和index不等的话则处理 while input_list[index] != index: # #如果input_list[index] == input_list[input_list[index]] 添加到res if input_list[index] == input_list[input_list[index]] : res.add(input_list[index]) break #跳出循环 #交换 else: tmp = input_list[index] input_list[index] = input_list[tmp] input_list[tmp] = tmp return res ##after check s = Solution() print s.find3([3,1,2,2,2,5,3]) #find1:space:O(n) time:O(1) #find2:space:O(1) time:O(nlog(n)) #find3:space:O(1) time:O(n)