LeetCode Single Number and Expansion

Source: Internet
Author: User

Knowledge Point: exclusive or operation

Idea: for example, the number B ^ B = 0

A ^ 0 =

Therefore, all numbers in the array can be exclusive or, And the eventually exclusive or result is the number a that appears only once.

Code:

      ret =      i          ret ^=      ret

 

 

 

 

: One of the numbers in an array appears only once, and the other numbers appear three times. Please find the number that appears only once?

)

0 1 0 0

0 1 1 1

0 1 0 0

0 1 0 0

 

The result 0111 obtained by Sum % 3 is 7, which is the required Single Number.

      ret =      i  range(0,32         c =         d = 1 <<          j               Array[j] &                 c += 1          c % 3             ret |=      ret

 

1, The second int's I-bit is 1, which indicates that the I-bit is accumulated 2.

0 1 0 0

One 0 1 0 0

Two 0 0 0 0

==================================

0 1 1 1

One 0 0 1 1

Two 0 1 0 0

==================================

0 1 0 0

One 0 0 0 0

Two 0 0 1 1

==================================

0 1 0 0

One 0 1 0 0

Two 0 0 0 0

==================================

Code:

     one =     two =     three =      i          two |= (one &         one ^=         three = one &         two -=         one -=      one

 

 

========================================================== ============================

:Two numbers a and B appear only once in an array, and the other numbers appear twice. Can you find the numbers that appear only once?

 

      ret =      i          ret ^=     pos =      j  range(0,32          (ret>>j) & 1             pos =                  sub1,sub2 =     single1 =     single2 =            sub1 =     sub2 =      i           (Array[i]>>pos) & 1                 sub1,sub2
      ret =      i          ret ^=     pos =      j  range(0,32          (ret>>i) & 1             pos =                  sub1,sub2 =     single1 =     single2 =            sub1 =     sub2 =      i           (Array[i]>>pos) & 1                 sub1,sub2

 

 

========================================================== ============================

:One digit, a, appears only once in an array and K in other numbers. Please find the number that appears only once?

Knowledge Point: Bit operation and score Solution

① K % 2 = 0

The basic idea is similar

② K % 2 = 1

The basic idea is similar

Code:

       k % 2 ==         ret =              ret =      ret

 

 

========================================================== ============================

 

: Three numbers a, B, and c appear only once in an array, and the other numbers appear twice. Can you find the three numbers that appear only once?

Idea: Here Reprinted from: http://zhedahht.blog.163.com/blog/static/25411174201283084246412/

If we convert all numbers in the array to different or, the final result (as x) returns the variance or result of the numbers a, B, and c (x = a ^ B ^ c ). The other two numbers are offset in the exclusive or operation.

 

We can prove that x is not one of the three distinct numbers a, B, and c.

We use the reverse verification method to prove this. Assume that x is equal to one of a, B, and c. For example, x equals to a, that is, a = a ^ B ^ c. Therefore, B ^ c is equal to 0, that is, B is equal to c. This is in conflict with the three distinct numbers a, B, and c.

 

Since x is different from a, B, and c, x ^ a, x ^ B, and x ^ c are not equal to 0.

 

We define a function f (n). The result is to retain the last 1 in the binary representation of the number n, and change all other bits to 0. For example, decimal 6 indicates that the binary value is 0110,

Therefore, the result of f (6) is 2 (Binary 0010 ). The results of f (x ^ a), f (x ^ B), and f (x ^ c) are not equal to 0.

Next we will consider the results of f (x ^ a) ^ f (x ^ B) ^ f (x ^ c. F (x ^ a) ^ f (x ^ B) the result of ^ f (x ^ c) is certainly not 0.

So f (x ^ a) ^ f (x ^ B) ^ f (x ^ c) has at least one binary value of 1. Assume that the last digit is 1 and the second digit is m. In the result of x ^ a, x ^ B, and x ^ c, the m-bit of one or three numbers is 1.

We use the arc method to prove that it is impossible for the three numbers to have 1 in the m position.

If the m-bits of x ^ a, x ^ B, and x ^ c are both 1, the m-bit numbers of numbers a, B, and c are the opposite of the m-bit numbers of x. Therefore, the m-bit numbers of numbers a, B, and c are the same. If the m-bits of the numbers a, B, and c are both

Is 0, x = a ^ B ^ c. The m-bit of the result is 0. Since the m-th digits of x and a are both 0, the m-th digit of the result of x ^ a should be 0. Likewise, it can be proved that x ^ B and x ^ c are both 0 at the m position. This is in conflict with our assumptions.

If the m-th digits of numbers a, B, and c are both 1, x = a ^ B ^ c, the m-th digit of the result is 1. Since the m-th digits of x and a are both 1, the m-th digit of the result of x ^ a should be 0.

Likewise, it can be proved that x ^ B and x ^ c are both 0 at the m position. This is still in conflict with our assumptions.

Therefore, only one of the three numbers x ^ a, x ^ B, and x ^ c has a 1-digit m-digit. So we found the criteria that can distinguish numbers a, B, and c.

========================================================== ==================================

 

Here we need to note that the above theoretical proof is only known in the case of a, B, c. The specific programming is somewhat different:

We first obtain x = a ^ B ^ c

We can compare x with the elements in the array again to obtain a new array, but the array feature is not changed.

For example, there are two equal numbers in the array d

After processing, the two numbers are equal, but the result is x ^ d.

 

========================================================== ======================================

In this way, we can divide the array into two parts. Here we assume that the m bit of a is 1.

Then the two sub-arrays are: sub1: including a =

Sub2: Contains B, c =

 

Code:

 

      x =      i          x ^=     newArray =      j          newArray.append(x^     lastOneArrary =     ret =      i          ret ^=     pos =      j  range(0,32          (ret>>j) & 1             pos =                  sub1, sub2 =                len(sub1)%2         single1 =         single2, single3 =              single1 =         single2, single3 =              newArray =      i          pos =          j  range(0,32              (Array[i]>>j) & 1                 pos =                          newArray.append(1<<      newArray

 

 

 

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.