An array lookup of "algorithm" spanning search

Source: Internet
Author: User

Topic 1: give a one-dimensional array A, size n, the absolute value of the difference between adjacent elements is 1. such as a = [1, 0, 1, 2, 3, 2, 1, 2, 3], now given a and target find Num. Please find the position of NUM in the array.
Topic 2: on the premise of topic one, now returns all index positions of Num.

idea: traversal can be implemented sequentially, but the complexity O (N).
If the first element of the array is a[0], the number to find is Num. Set T = ABS (Num-a[0]). Since the absolute value of each adjacent number is 1, the number before the T-position is definitely smaller than Num. So the next search is anchored to a[t]. Recalculate T, T = ABS (Num-a[t]), repeat the above steps. The difference between the current position and the lookup number is used to achieve a leap-forward search. This search method is a bit higher than traversing an array.
Topic one and topic two is actually a problem, the topic after finding the element is returned, the topic two to search until the end of the array.

AC Code:

#include <iostream>#include <stdio.h>#include <math.h>#include <vector>using namespace STD;intFindnuminarray (intArr[],intNintNUM) {intNextindex =ABS(num-arr[0]); while(Nextindex < N) {if(Arr[nextindex] = = num)returnNextindex; Nextindex + =ABS(Num-arr[nextindex]); }return-1;}voidFindallnuminarray (intArr[],intNintNum vector<int>& ret) {intNextindex =ABS(num-arr[0]); while(Nextindex < N) {if(Arr[nextindex] = = num)            {Ret.push_back (Nextindex);        ++nextindex; }Else{Nextindex + =ABS(Num-arr[nextindex]); }    }return;}voidMain () {Const intMaxLen =Ten;intArr[maxlen] = {1,0,1,2,3,2,1,2,3,4};//Test topic One, find the index of the first matching number    printf("Find%d, \ t subscript%d, \ t is actually subscript 0\n",1, Findnuminarray (arr, MaxLen,1));printf("Find%d, \ t subscript%d, \ t is actually subscript 4\n",3, Findnuminarray (arr, MaxLen,3));printf("Find%d, \ t subscript%d, \ t is actually subscript 9\n",4, Findnuminarray (arr, MaxLen,4));printf("Find%d, \ t subscript%d, \ t is actually subscript -1\n",-1, Findnuminarray (arr, MaxLen,-1));//Test topic Two, return all indexes     vector<int>Ret Findallnuminarray (arr, MaxLen,1, ret);if(!ret.empty ()) { for( vector<int>:: Size_type i =0; I < ret.size (); i++)printf("Look for index%d \ t subscript%d",1, Ret[i]); } getchar ();return;}

Operation Result:

An array lookup of "algorithm" spanning search

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.