Skip to content

Commit 78f0941

Browse files
authored
至少是其他数字两倍的最大数
1 parent 3232369 commit 78f0941

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

算法/LeetCode/dominantIndex.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
5+
int dominantIndex(int *nums, int numsSize)
6+
{
7+
// if(nums == NULL || numsSize == 0)
8+
// return -1;
9+
#if 0
10+
int i,j,temp;
11+
12+
for(i = 0; i < numsSize - 1; i++)
13+
{
14+
for(j = 0; j < numsSize - i - 1; j++)
15+
{
16+
if(nums[j] > nums[j + 1])
17+
{
18+
temp = nums[j + 1];
19+
nums[j + 1] = nums[j];
20+
nums[j] = temp;
21+
}
22+
}
23+
}
24+
25+
for(i = 0; i < numsSize; i++)
26+
{
27+
printf("%d\n",nums[i]);
28+
}
29+
#endif
30+
31+
#if 0
32+
int max1 = nums[numsSize - 1];
33+
int max2 = nums[numsSize - 2];
34+
printf("max1 = %d\n max2 = %d\n",max1,max2);
35+
36+
37+
if(max2 != 0 && max1/max2 < 2)
38+
return -1;
39+
40+
i = 0;
41+
for(j = 0; j < numsSize; j++)
42+
{
43+
if(nums[j] == max1)
44+
i = j;
45+
return i;
46+
}
47+
return i;
48+
#endif
49+
50+
#if 0
51+
int max = nums[numsSize - 1];
52+
i = 0;
53+
for(j = 0; j < numsSize;j++)
54+
{
55+
if(nums[j]!=max && nums[j] != 0 && max/nums[j] < 2)
56+
return -1;
57+
else if(nums[j] == max)
58+
i = j;
59+
}
60+
return i;
61+
#endif
62+
63+
if(nums == NULL || numsSize == 0)
64+
return -1;
65+
if(numsSize == 1)
66+
return 0;
67+
68+
int max1=0;
69+
int max2 = 0;
70+
int i,k = 0;
71+
for(i = 0; i < numsSize; i++)
72+
{
73+
if(nums[i] > max1)
74+
{
75+
max2 = max1;
76+
max1 = nums[i];
77+
k = i;
78+
}
79+
else if(nums[i] > max2)
80+
{
81+
max2 = nums[i];
82+
}
83+
}
84+
85+
if(max2 == 0 || max1 >= 2*max2)
86+
return k;
87+
88+
else
89+
return -1;
90+
91+
}
92+
93+
94+
int main(int argc, const char *argv[])
95+
{
96+
int str[] = {3,6,0,1};
97+
98+
int num;
99+
100+
num = dominantIndex(str,4);
101+
102+
printf("num = %d\n",num);
103+
return 0;
104+
}

0 commit comments

Comments
 (0)