Wins the Offer (28): More than half of the number of array numbers appear
Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
GitHub: https://github.com/ aimi-cn / AILearners
First, the primer
This series is my brush "to prove safety Offer" brush off the cattle in question notes online, it aims to enhance the ability under its own algorithm.
View the complete algorithm to prove safety issues resolved Offer Click CSDN and github link:
prove safety Offer complete analytical exercises CSDN address
github address
Second, the title
There are a number of array number that appears more than half the length of the array, find this number. For example, a length of the input array 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears five times, more than half the length of the array in the array, the output 2. If there is 0 output.
1, ideas
1 idea: to sort the array to find the number in the middle of the array. If the number of times a number appears more than half the length of the array, then after ordering this number is certainly among the best, then count the number of ways to view the number of occurrences is greater than half the length of the array, then it returns this number, and 0 otherwise.
Ideas 2: If the number has qualified a number, and it appears even more than the number of all the other numbers appear.
Save when traversing an array of two values: one is a numeric array, one number. When traversing the next number, if it is previously stored in the same figure, plus the number 1 or decremented by 1; if the number is 0, the next number is saved, and the number is set to 1. After traversing the saved numbers is also desired. And then determine whether it meets the conditions can be.
This actually is a "fractal leaf" method, the array simultaneously remove two different numbers, the last remaining of a number is the number. If the remaining two, then the two is the same, is the result, on the basis of its last remaining digit or two back to the original array, the array traversal again statistics about the numbers of occurrences for final judgment.
2, programming
python
Code implementation:
1 idea:
# -*- coding:utf-8 -*-
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
numbers.sort()
theone = numbers[len(numbers)/2]
if numbers.count(theone) > len(numbers)/2:
return theone
return 0
2 ideas:
# -*- coding:utf-8 -*-
class Solution:
def MoreThanHalfNum_Solution(self, numbers):
# write code here
if not numbers:
return 0
res = numbers[0]
times = 1
length = len(numbers)
for i in range(1, length):
if times == 0:
res = numbers[i]
times = 1
elif res == numbers[i]:
times += 1
else:
times -= 1
import collections
return res if collections.Counter(numbers)[res] * 2 > length else 0
AIMI-CN AI learning exchange group [1015286623] for more information on AI
Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!