C language to find the specific elements in the array algorithm parsing _c language

Source: Internet
Author: User

     problem Description: An int array with no restrictions on the data, requiring all such numbers to be a[i], the number on the left is less than or equal to it, and the number on the right is greater than or equal to it. Can be implemented with only one extra array and a small amount of other space.
      Idea: If you can use two auxiliary arrays, then relatively simple, you can define the array min and the group Max, where Min[i] represents the minimum value (including A[i] after A[i), Max[i] Represents the maximum value of an element before A[i]. With these two auxiliary arrays, for a[i], if it is greater than max[i-1 and less than min[i+1], then the requirement is met.
      But the title requirement is to use only an extra array, in fact, the max array can be omitted, completely can be judged side of the calculation, this is because Max[i] is calculated from the left to the right, and the judgement is from left to right, Two processes can just fit together. Just use a variable max to save the current maximum value. The following is a code implementation for two methods.
      Reference Code:

/function function: find element//function parameter: Parray point to Array, Len number of elements//return value: no void findelements_solution1 (int *parray 
 
  , int len) {if (Parray = NULL | | | len <= 0) return; 
  int *pmin = new Int[len]; 
  int *pmax = new Int[len]; 
 
  int i; 
  Pmax[0] = parray[0]; for (i = 1; i < Len; i++)//The auxiliary array pmax[i] = (pmax[i-1] >= parray[i)? 
  PMAX[I-1]: parray[i]; 
  Pmin[len-1] = parray[len-1]; for (i = len-2 i >= 0; i--)//The auxiliary array pmin[i] = (pmin[i+1) <= parray[i]? 
 
  PMIN[I+1]: parray[i]; 
  if (Parray[0] <= pmin[0])//Check whether the 1th element satisfies the condition cout<<parray[0]<< '; for (i = 1; i < len-1 i++) {if (Parray[i] >= pmax[i-1] && parray[i] <=pmin[i+1])//satisfies the element of this relationship 
  Require cout<<parray[i]<< '; 
  } if (Parray[len-1] >= pmax[len-1])//Check that the Len element satisfies the condition cout<<parray[i]; 
 
  cout<<endl; 
  delete [] pMin; 
  delete [] Pmax; 
PMin = Pmax = NULL; } 

 void Findelements_solution2 (int *parray, int len) {if (Parray = NULL | | Len <= 0 
 
  ) return; 
  int *pmin = new Int[len]; 
  int Max; 
 
  int i; 
  Max = parray[0]; 
  Pmin[len-1] = parray[len-1]; for (i = len-2 i >= 0; i--)//The auxiliary array pmin[i] = (pmin[i+1) <= parray[i]? 
 
  PMIN[I+1]: parray[i]; 
 
  if (Parray[0] <= pmin[0])//Check whether the 1th element satisfies the condition cout<<parray[0]<< '; 
      for (i = 1; i < len-1 i++) {if (parray[i) >= Max && parray[i] <=pmin[i+1]//Meet the elements of this relationship to meet the requirements 
    cout<<parray[i]<< '; max = (Max < parray[i])? Parray[i]: Max; 
  Update current Maximum} if (Parray[len-1] >= max)//check that the Len element satisfies the condition cout<<parray[i]; 
 
  cout<<endl; 
  delete [] pMin; 
PMin = NULL; } 

Finds two occurrences of a number in an array (array)
problem Description: In an integer array, except for two digits, the other numbers appear two times. Please write a program to find out the two numbers that appear only once. Required time complexity is O (n), space complexity is O (1).
Train of thought: If only one number appears once, and all the others appear two times, then all the numbers will be made one at a time, because the equal number is different or the result is 0. If two numbers appear only once, and other numbers appear two times. What should we do? The book "The Beauty of programming" provides a way to that is, all the numbers are first made a different or operation, get a number, and then the number of a 0-bit as a filter bit, the array into two parts, at this time only one occurrence of the number will be divided into parts. Now the problem is changed to only one occurrence, each part of the difference or operation can be.
Reference code:

function function: To find two occurrences of the number 
//function parameter in the array: arr is the source array, Len is the number of elements, and result is used to store the results  
//return value:  no 
void findisolatetwo (int * arr, int len, int *result) 
{ 
  int i, all = 0, flag = 1; 
 
  for (i = 0; i < len; i++)//All ^= or all 
    arr[i]; 
 
  while (!) ( All&flag))//Looking for filter bit 
    flag <<= 1; 
 
  Result[0] = result[1] = 0; 
  for (i = 0; i < len; i++)//Use filter bit to differentiate 
  { 
    if (Flag&arr[i]) 
      result[0] ^= arr[i]; 
    else 
      result[1] ^= arr[i]; 
  } 
 

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.