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 数组中出现次数超过一半的数字的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复