Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b4586a3

Browse files
Single number
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent 5b54484 commit b4586a3

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

136_single_number/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test single_number.c

136_single_number/single_number.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static int singleNumber(int *nums, int numsSize)
5+
{
6+
int i, s = 0;
7+
for (i = 0; i < numsSize; i++) {
8+
s ^= nums[i];
9+
}
10+
return s;
11+
}
12+
13+
int main(int argc, char **argv)
14+
{
15+
int i, count = argc - 1;
16+
int *nums = malloc(count * sizeof(int));
17+
for (i = 0; i < count; i++) {
18+
nums[i] = atoi(argv[i + 1]);
19+
}
20+
printf("%d\n", singleNumber(nums, count));
21+
return 0;
22+
}

137_single_number_ii/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test single_number.c

137_single_number_ii/single_number.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#if 0
5+
static void singleNumber(int *nums, int numsSize)
6+
{
7+
int i, xor = 0;
8+
for (i = 0; i < numsSize; i++) {
9+
xor ^= nums[i];
10+
}
11+
12+
int mask = 1;
13+
while (!(mask & xor)) {
14+
mask <<= 1;
15+
}
16+
17+
int zero = 0;
18+
int one = 0;
19+
for (i = 0; i < numsSize; i++) {
20+
if (nums[i] & mask) {
21+
one ^= nums[i];
22+
} else {
23+
zero ^= nums[i];
24+
}
25+
}
26+
27+
printf("%d %d\n", zero, one);
28+
}
29+
#endif
30+
31+
static int singleNumber(int *nums, int numsSize)
32+
{
33+
#if 1
34+
int i, j, count[32], mask = 0;
35+
for (i = 0; i < 32; i++) {
36+
count[i] = 0;
37+
for (j = 0; j < numsSize; j++) {
38+
if ((1 << i) & nums[j]) {
39+
count[i]++;
40+
}
41+
}
42+
mask |= (count[i] % 3) << i;
43+
}
44+
return mask;
45+
#else
46+
int i, ones = 0, twos = 0, threes = 0;
47+
for (i = 0; i < numsSize; i++) {
48+
/* `ones & nums[i]` the result is the bitmask which the bits appeared twice */
49+
twos |= ones & nums[i];
50+
/* reset mask twice in `ones` */
51+
ones ^= nums[i];
52+
/* count the `three` */
53+
threes = ones & twos;
54+
/* clear the `ones` and `twos` if the i-th bit had appeared three times. */
55+
ones &= ~threes;
56+
twos &= ~threes;
57+
}
58+
return ones;
59+
#endif
60+
}
61+
62+
int main(int argc, char **argv)
63+
{
64+
int i, count = argc - 1;
65+
int *nums = malloc(count * sizeof(int));
66+
for (i = 0; i < count; i++) {
67+
nums[i] = atoi(argv[i + 1]);
68+
}
69+
printf("%d\n", singleNumber(nums, count));
70+
return 0;
71+
}

0 commit comments

Comments
 (0)