Array problem 1. Find 2 in a two-dimensional array of rows and columns. There are two numbers appearing once in a set of number pairs that appear 3. Find a number in the shift array

Source: Internet
Author: User

First, in a two-dimensional array, each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.

Ideas:

First, select the number in the upper-right corner of the array. If the number is equal to the number you are looking for, the lookup process ends, if the number is greater than the number you are looking for, the column that contains the number is excluded, and if the number is less than the number you are looking for, the row that contains the number is excluded. That is, if the number you are looking for is not in the upper-right corner of the array, each time you look at the array, it will exclude one row or column, so that each step narrows the search.

The implementation process is as follows:

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/80/11/wKiom1c11Xfzs0-WAAAN-uL1gHo691.jpg "style=" float: none; "Title=" L4. JPG "alt=" Wkiom1c11xfzs0-waaan-ul1gho691.jpg "/>

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/80/0E/wKioL1c11lqx8yC4AABxCUYvO1E228.jpg "style=" float: none; "Title=" L2. JPG "alt=" Wkiol1c11lqx8yc4aabxcuyvo1e228.jpg "/>

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/80/11/wKiom1c11XfyrQBoAAA87hOqtYg329.jpg "style=" float: none; "Title=" L3. JPG "alt=" Wkiom1c11xfyrqboaaa87hoqtyg329.jpg "/>

The results of the operation are as follows:

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/80/11/wKiom1c11XjSHegUAAANb2w3sGM123.jpg "style=" float: none; "Title=" L1. JPG "alt=" Wkiom1c11xjsheguaaanb2w3sgm123.jpg "/>

Second, in a set of integers, only two numbers appear once, the other numbers are in pairs appear.

For example: array[10]={1,1,2,2,3,3,5,7,4,4}

Idea: We all know that a number is different from itself or zero, with 0 XOR or for it itself. So we can let this group of numbers be different or, the result will not be zero, if I. Then, from right to left, find the first bit of the binary of I in 1, assuming that the count bit is shifted to the right. Then let each number in the array right-shift the count bit on this one with I, the result is 1 for a group, the remainder for another group, and then on the separate or. Results can be obtained.

The implementation process is as follows:

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/80/0E/wKioL1c116nAVKhtAAAN-uL1gHo479.jpg "style=" float: none; "Title=" L4. JPG "alt=" Wkiol1c116navkhtaaan-ul1gho479.jpg "/>

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/80/11/wKiom1c11sfzUTYdAACExbtJWGM024.jpg "style=" float: none; "Title=" L2. JPG "alt=" Wkiom1c11sfzutydaacexbtjwgm024.jpg "/>

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/80/0E/wKioL1c116qzLAeKAACF8yceVUQ935.jpg "style=" float: none; "Title=" L3. JPG "alt=" Wkiol1c116qzlaekaacf8ycevuq935.jpg "/>

Operation Result:

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/80/11/wKiom1c11siQHceLAAALeBCmjmQ339.jpg "style=" float: none; "Title=" L1. JPG "alt=" Wkiom1c11siqhcelaaalebcmjmq339.jpg "/>

If the array is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/80/0E/wKioL1c12NfwhhpBAAAP80HJ1Gg488.jpg "style=" float: none; "Title=" L1. JPG "alt=" Wkiol1c12nfwhhpbaaap80hj1gg488.jpg "/>

Operation Result:

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/80/11/wKiom1c11_WjAU2eAAALAuCXqtQ330.jpg "style=" float: none; "Title=" L2. JPG "alt=" Wkiom1c11_wjau2eaaalaucxqtq330.jpg "/>

There is a set of arrays, sorted from small to large, that have been shifted and become as follows.

array[10]={0,1,2,3,4,5,6,7,8,9};

After two-bit right shift:array[10]={2,3,4,5,6,7,8,9,0,1}

after four-bit right shift: array[10]={4,5,6,7,8,9,0,1,2,3,}

after five-bit right shift: array[10]={5,6,7,8,9,0,1,2,3,4}

Please design a function, enter a number to find out if it is in the shifted array, do not know that the array after several shifts, requires less time complexity (O (n)).

Because of the complexity of the time required so can not traverse, although the array has been shifted, but in fact, is divided into two ordered parts. The main point is to determine the number of bits to find in which part, which is essentially a binary lookup, if Array[mid]<array[left], the right side of Array[mid] is an ordered sequence, and vice versa that it is an ordered sequence on the left. Then compare it with the number you are looking for.

The specific implementation is as follows:

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M02/80/0E/wKioL1c14Izjjx4oAAAN-uL1gHo953.jpg "style=" float: none; "Title=" L4. JPG "alt=" Wkiol1c14izjjx4oaaan-ul1gho953.jpg "/>

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/80/12/wKiom1c136qyJfdmAACMI6kwyZ0523.jpg "style=" float: none; "Title=" L1. JPG "alt=" Wkiom1c136qyjfdmaacmi6kwyz0523.jpg "/>

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/80/0E/wKioL1c14I2ij7G_AACYD8hk-c4578.jpg "style=" float: none; "Title=" L2. JPG "alt=" Wkiol1c14i2ij7g_aacyd8hk-c4578.jpg "/>

The results of the operation are as follows:

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M02/80/12/wKiom1c136vA2Y4pAAANYktXw20976.jpg "style=" float: none; "Title=" L3. JPG "alt=" Wkiom1c136va2y4paaanyktxw20976.jpg "/>




This article is from the "fringe" blog, so be sure to keep this source http://ab6107.blog.51cto.com/10538332/1773187

Array problem 1. Find 2 in a two-dimensional array of rows and columns. There are two numbers appearing once in a set of number pairs that appear 3. Find a number in the shift 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.