概述
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 数组中出现次数超过一半的数字所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复