Last active
January 1, 2016 12:19
-
-
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 然后让你找出数字中重复最多的一个数字。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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