The number of times in the array "Sword refers to offer" exceeds half the length of the array.

Source: Internet
Author: User

The number of times in the array "Sword refers to offer" exceeds half the length of the array.

Question: 
There is a number in the array that appears more than half the length of the array. Please find this number. For example, enter an array {, 2} with a length of 9 }. Because number 2 appears five times in the array and exceeds half the length of the array, Output 2. If it does not exist, 0 is output.
Resolution: 
When I see this question, I first want to sort the array and search for the correct number of questions in the sorted array. This method is feasible, but this method is too obvious, and the sorting time complexity is greater than O (n), I feel the interviewer will not agree. So I thought of comparing the number of I with the number of I + 1. If the number is the same, write down this number and add one to the counter. If the number is the same, subtract one from the difference. When the counter is zero, record this number; if the counter is zero, there is no such number. Otherwise, it may exist. traverse it again and compare the number recorded with all the numbers in the array, finally, if the same number is greater than the length of the array, this number is generally returned. Otherwise, 0 is returned. the time complexity of this method is O (n );
Below I will write down the code for both methods and share it with you.

Method 1 code:

1 int MoreThanHalfNum_Solution (int * arr, int len)
  2 {
  3 int i, j, flag = 1, count = 1;
  4 for (i = 0; i <len-1; i ++) // sort the array
  5 {
  6 for (j = 0; j <len-1-i; j ++)
  7 {
  8 if (arr [j]> arr [j + 1])
  9             {
10 int tmp = arr [j];
11 arr [j] = arr [j + 1];
12 arr [j + 1] = tmp;
13 flag = 0;
14}
15}
16 if (flag)
17 break;
18}
19 for (i = 0; i <len; i ++) // Walk through to find if there is a number that matches the intent of the question
20 {
21 if (arr [i] == arr [i + 1])
twenty two         {
23 j = i;
24 count ++;
25 if (count> (len >> 1))
26 {
27 return arr [j];
28}
29}
30 else
31 {
32 count = 1;
33}
34}
35 return 0;
36}

Method 2 code:

1 int MoreThanHalfNum_Solution (int * arr, int len)
  2   {
  3 int i = 1;
  4 int k = 0;
  5 int count = 1;
  6 while (i <len)
  7 {
  8 if (arr [i] == arr [k])
  9           {
10 k = i; // Record the current position
11 count ++;
12}
13 else
14 {
15 count-; // Different if the counter is decremented
16 if (count == 0)
17 {
18 count = 1;
19 k = i;
20}
twenty one           }
22 i ++;
twenty three       }
24 if (count)
25 {
26 count = 0;
27 for (i = 0; i <len; i ++)
28 {
29 if (arr [k] == arr [i])
30 count ++;
31}
32 if (count> len / 2)
33 return arr [k];
34 else
35 return 0;
36}
37 else
38 return 0;
39} 

CSDN blog address: http://blog.csdn.net/qq_38646470

 


Related Article

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.