In an array of '75 int' type, except for one or two occurrences, all other occurrences occur three times.

Source: Internet
Author: User

[Link to this article]

Http://www.cnblogs.com/hellogiser/p/single-number-of-array-with-other-three-times.html

[Question]

In an array of the int type, each number appears three times except one or two times.

[Analysis]

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
  Int singlenumber (int * a, int N)
{
Int ones = 0, twos = 0;
For (INT I = 0; I <n; I ++)
{
Ones = (ones ^ A [I]) & (~ Twos );
Twos = (twos ^ A [I]) & (~ Ones );
}
// Ones
Return ones;
}

When a appears, ones can save. When a appears twice, twos can save. When a appears three times, both ones and twos are cleared. Therefore, if all the numbers in a value are passed through this loop, the three numbers are cleared. If one number exists once, it is stored in ones; if it appears twice, it is saved in twos.

[Code]

 

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  // 75_onetwonumber_withother3times.cpp: defines the entry point for the console application.
//
/*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/9/18
*/

# Include "stdafx. H"
# Include "iostream"
Using namespace STD;

Void print (int * a, int N)
{
For (INT I = 0; I <n; I ++)
{
Cout <A [I] <"";
}
Cout <Endl;
}

// ================================================ ======================================
// Single number 1, 1, 2 => 2
// ================================================ ======================================
/*
X 1st, ones = x, twos = 0
X 2st, ones = 0, twos = x
X 3st, ones = 0, twos = 0
*/
Int singlenumber (int * a, int N)
{
Int ones = 0, twos = 0;
For (INT I = 0; I <n; I ++)
{
Ones = (ones ^ A [I]) & (~ Twos );
Twos = (twos ^ A [I]) & (~ Ones );
}
// Ones
Return ones;
}

// ================================================ ======================================
// Double number 1,100,100, 100 ======>
// ================================================ ======================================
Int doublenumber (int * a, int N)
{
Int ones = 0, twos = 0;
For (INT I = 0; I <n; I ++)
{
Ones = (ones ^ A [I]) & (~ Twos );
Twos = (twos ^ A [I]) & (~ Ones );
}
// Twos
Return twos;
}

Void test_base_single ()
{
Int A [] = {1, 1, 1, 2 };
Int N = 4;
Cout <"array is :";
Print (A, N );
Cout <"single number" <singlenumber (A, n) <Endl;
}

Void test_base_double ()
{
Int A [] = {1, 1, 1,100,100 };
Int n = 5;
Cout <"array is :";
Print (A, N );
Cout <"Double number" <doublenumber (A, n) <Endl;
}

Void test_main ()
{
Test_base_single ();
Test_base_double ();
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Test_main ();
Return 0;
}
/*
Array is: 1 1 1 2
Single Number 2
Array is: 1 1 100 100
Double number 100
*/

In an array of '75 int' type, except for one or two occurrences, all other occurrences occur three times.

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.