Sword means offer interview 29-number of occurrences more than half in the array

Source: Internet
Author: User

Tag: Sword refers to a number of more than half occurrences in an offer array partition

Topic:

If there is a number in the array that appears more than half the length of the array, find this number. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears in the array 5 times, which exceeds half the length of the array, the output is 2.


Solution One:

The array is sorted first, and then the number of occurrences more than half is a[n/2+1], and the time Complexity O (NLGN).


Solution Two: O (n)

Basic idea:

elimination principle: in traversingArray, save two values: One is a number in an array, and the other is the number of times. When we traverse to the next number, if the next number is the same as the number we saved before, the number of times is added1. If the next number is different from the number we saved before, the number of times is reduced1. If the number is zero, we need to save the next number and set the number to1. Since the number we are looking for appears to be more than the sum of the number of other numbers appearing, the number to be searched is definitely the last to be set to1the corresponding number.

Of course, the final step is to verify that the number of occurrences is greater than half of the array.

#include <iostream>using namespace Std;bool checkmorethanhalf (int a[],int len,int key) {int times=0;for (int i=0;i& lt;len;i++) {if (A[i]==key) times++;} BOOL Flag=true;if (Times*2<=len) Flag=false;return flag;} int foo (int a[],int len) {if (len<=0) return-1;int result=a[0];int times=2;for (int i=1;i<len;i++) {if (times==0) { Result=a[i];times=1;} else if (A[i]==result) times++;elsetimes--;} if (! Checkmorethanhalf (A,len,result)) Result=0;return result;} int main () {int a[]={1,2,3,2,2,2,5,4,2};int b[]={1,2,3,4};int LenA = sizeof (a)/sizeof (a[0]); int lenB = sizeof (b)/sizeof ( B[0]); Cout<<foo (A,lena) <<endl;cout<<foo (B,LENB) <<endl;return 0;}



Solution Three: Based on the partition function, O (n)

Basic idea:

If the number of occurrences in the array exceeds half the length of the array, then the array is sorted, and the number in the middle of the array is the number that appears more than half. The time complexity of sorting an array is O (Nlog (n)), but for this problem there is a better algorithm that can be found in time complexity O (n). We wrote the fast sort algorithm, where the partition () method is the most important method, the method returns an index that guarantees that the number of index positions is sorted, and that the number on the left side of index is smaller than the number of index. The number to the right of index is larger than the number of index. Then the subject can be used to solve the idea.

    1. Returns index by partition (), if Index==mid, indicates that the median of the array is found, and if index<mid indicates that the median is between [index+1,end] and if index>mid indicates that the median is in [ START,INDEX-1]. Know that the end of the Index==mid cycle is obtained.
    2. According to the evaluated index, iterate through the array, whenever a time++ is equal to the number pointed to by index, and finally determine whether the time is greater than half the length of the array, if greater than the number pointed to index is the number that is asked, if not, Indicates that there is no more than half of the number of occurrences of the array.

#include <iostream>using namespace Std;bool checkmorethanhalf (int a[],int len,int key) {int times=0;for (int i=0;i& lt;len;i++) {if (A[i]==key) times++;} BOOL Flag=true;if (Times*2<=len) Flag=false;return flag;}      /*int par (int a[],int len,int low,int high) {int t=a[low];          while (Low



Sword means offer interview 29-number of occurrences more than half in the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.