def partition(data, length, start, end): if data == None or length == 0 or start < 0 or end >= length: return False tmp = end small = start - 1 for index in range(start, end): if data[index] < data[end]: small += 1 small += 1 return small def more_than_half_num(number, length): if number == None or length <= 0: return False middle = length >> 1 start = 0 end = length -1 index = partition(number, length, start, end) if index == None: print("useless operation.") while index != middle: if index > middle: end = index - 1 index = partition(number, length, start, end) else: start = index + 1 index = partition(number, length, start, end) result = number[index] print(result) return result def more_than_half_num_first(number): length = len(number) more_than_half_num(number, length) number = [1, 2, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ] if __name__ == "__main__": more_than_half_num_first(number)
python 数组中出现次数超过一半的数字
猜你喜欢
转载自blog.csdn.net/z2539329562/article/details/80413124
今日推荐
周排行