Skip to content

Instantly share code, notes, and snippets.

@oldcai
Last active January 1, 2016 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldcai/8144123 to your computer and use it in GitHub Desktop.
Save oldcai/8144123 to your computer and use it in GitHub Desktop.
题目是这样: 有一个队列,比方说是:2,2,2,2,2,2,3,3,3,3,3,1,1,1,5,5,5,5 然后让你找出数字中重复最多的一个数字。
#coding: utf-8
def get_max_repeat(*arr):
length = len(arr)
if length == 0:
return 0, 0
last_index = -1
current_index = 0
max_repeat_number = arr[0]
max_repeat = 1
while current_index < length:
current_number = arr[current_index]
if current_index + max_repeat < length \
and arr[current_index] == arr[current_index + max_repeat]:
current_index += max_repeat
while current_index + 1 < length \
and current_number == arr[current_index + 1]:
current_index += 1
step = 1
while current_index + step * 2 < length \
and current_number == arr[current_index + step * 2]:
step *= 2
if 1 != step:
current_index += step
current_repeat = current_index - last_index
if current_repeat > max_repeat:
max_repeat = current_repeat
max_repeat_number = current_number
last_index = current_index
current_index += 1
return max_repeat, max_repeat_number
if __name__ == '__main__':
print get_max_repeat(2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 5, 5, 5, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment