暖身題,piece of cake. (真是大言不慚XD)
先sort 過了,有改過值再存回去果然會忘記它們排序會改變Orz 慎之!!!
int comp (const void *a, const void *b)
{
return (*(int*)b-*(int*)a);
}
void swap (int *a, int *b)
{
int tmp= *a;
*a = *b;
*b = tmp;
return;
}
int lastStoneWeight(int* stones, int stonesSize) {
if (stonesSize <=1)
return stones[0];
qsort(stones, stonesSize, sizeof(int), comp);
int i=1;
for (i=1; i<stonesSize; i++)
{
if (stones[i-1]== stones[i])
i++;
else if (stones[i-1]> stones[i])
{
stones[i]= stones[i-1] - stones[i];
for (int j=i;j<stonesSize-1; j++)
{
if (stones[j]<stones[j+1])
swap(&stones[j], &stones[j+1]);
else
break;
}
}
}
if (i > stonesSize)
return 0;
return stones[stonesSize-1];
}
沒有留言:
張貼留言