python实现代码:
class Solution: def find(self,nums): ret = [] sum = 0 for num in nums: sum += num; avr = sum/len(nums) distance=[] for num in nums: distance.append(abs(num-avr)) i = 0 flag = [] retflag = [] while i < 3: data=distance[0] m = 0 n = 0 for num in distance: if num < data and num not in flag: data = num n = m m += 1 flag.append(data) retflag.append(n) i+=1 for num in retflag: ret.append(nums[num]) return ret if __name__ == '__main__': test=Solution() a=[33,44,61,2,36,42,56,81,11,17] b=test.find(a) print b
运行结果: