-
Notifications
You must be signed in to change notification settings - Fork 0
/
offer-53-01-NumberOfK.c
77 lines (67 loc) · 1.57 KB
/
offer-53-01-NumberOfK.c
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
#include <stdio.h>
// using two pointers
int search(int *nums, int numsSize, int target)
{
if (nums == NULL)
{
return 0;
}
int begin = -1, end = -1;
for (int i = 0; i < numsSize; i++)
{
if (nums[i] == target && begin == -1)
{
begin = i;
}
else if (nums[i] != target && begin >= 0)
{
end = i;
break;
}
else if (i == numsSize - 1)
{
end = i + 1;
}
}
if (begin == -1)
{
return 0;
}
else if (begin >= 0 && end <= 0)
{
return 1;
}
else
{
return end - begin;
}
}
// simple solution, concise
int search1(int *nums, int numsSize, int target)
{
int cnt = 0;
for (int i = 0; i < numsSize; i++)
{
cnt = (nums[i] == target ? cnt + 1 : cnt);
}
return cnt;
}
int main()
{
int a[] = {1, 2, 3};
printf("%d\n", search(a, (int)(sizeof(a) / sizeof(a[0])), 2));
printf("%d\n", search1(a, (int)(sizeof(a) / sizeof(a[0])), 2));
int b[] = {1, 2, 3, 3, 3, 4, 5};
printf("%d\n", search(b, (int)(sizeof(b) / sizeof(b[0])), 3));
printf("%d\n", search1(b, (int)(sizeof(b) / sizeof(b[0])), 3));
int c[] = {1, 2, 3, 3, 3, 3, 3};
printf("%d\n", search(c, (int)(sizeof(c) / sizeof(c[0])), 3));
printf("%d\n", search1(c, (int)(sizeof(c) / sizeof(c[0])), 3));
int d[] = {1, 2, 3, 3, 3, 3, 3};
printf("%d\n", search(d, (int)(sizeof(d) / sizeof(d[0])), 10));
printf("%d\n", search1(d, (int)(sizeof(d) / sizeof(d[0])), 10));
int f[] = {1};
printf("%d\n", search(f, (int)(sizeof(f) / sizeof(f[0])), 1));
printf("%d\n", search1(f, (int)(sizeof(f) / sizeof(f[0])), 1));
return 0;
}