1.单调数列
//单调数列
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
//递增
bool Inc_Seq(int *arr, int num)
{
bool res = true;
for (int i = 0; i < num-1; i++)
{
if (arr[i]>arr[i + 1])
{
res = false;
break;
}
}
return res;
}
//递减
bool Dec_Seq(int *arr, int num)
{
bool res = true;
for (int i = 0; i < num - 1; i++)
{
if (arr[i] < arr[i + 1])
{
res = false;
break;
}
}
return res;
}
bool Is_Monoton(int *arr, int num)
{
assert(arr != NULL);
return Inc_Seq(arr, num) || Dec_Seq(arr, num);
}
int main()
{
int arr[] = { 12, 23, 34, 45, 56, 67, 78, 89, 90, 100 };
int brr[] = { 100, 90, 89, 78, 67, 56, 45, 34, 23, 12 };
int crr[] = { 89, 78, 65, 23, 12, 58, 64, 67, 45 };
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d \n", Is_Monoton(arr, size));
size = sizeof(brr) / sizeof(brr[0]);
printf("%d \n", Is_Monoton(brr, size));
size = sizeof(crr) / sizeof(crr[0]);
printf("%d \n", Is_Monoton(crr, size));
system("pause");
return 0;
}
2.数组中寻找重复的数
暴力破解法:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int Find_Duplicate_Arr(int *arr, int size)
{
assert(arr != NULL);
int tmp = 0;
for (int i = 0; i < size; i++)
{
tmp = arr[i];
for (int j = i + 1; j < size; j++)
{
if (tmp == arr[j])
{
goto end;
}
}
}
end:
return tmp;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 2, 2 };
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d \n", Find_Duplicate_Arr(arr, size));
system("pause");
return 0;
}
3.将数组有序后,寻找重复元素
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int Compar_ints(const void *a, const void *b)
{
int arg1 = *(const int *)a;
int arg2 = *(const int *)b;
if (arg1 < arg2)
{
return 1;
}
else if(arg1>arg2)
{
return -1;
}
else
{
return 0;
}
}
int Find_Duplicate_Sort_Arr(int *arr, int size)
{
assert(arr != NULL);
qsort(arr, size, sizeof(int), Compar_ints);
int tmp = 0;
for (int i = 0; i < size - 1; i++)
{
if (arr[i] == arr[i + 1])
{
tmp = arr[i];
break;
}
}
return tmp;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 3, 7 };
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d \n", Find_Duplicate_Sort_Arr(arr, size));
system("pause");
return 0;
}
4.查表法---以空间换时间
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int Find_Duplicate_Table_Method_Arr(int *arr, int count)
{
assert(arr != NULL);
int*newdata = (int *)calloc(count,sizeof(int));
if (newdata == NULL)
{
return -1;
}
int idx = 0;
for (int i = 0; i < count; i++)
{
if (newdata[arr[i]] == 0)
{
newdata[arr[i]] = 1;
}
else
{
idx = arr[i];
break;
}
}
free(newdata);
newdata = NULL;
return idx;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 4 };
const int count = sizeof(arr) / sizeof(arr[0]);
printf("%d \n", Find_Duplicate_Table_Method_Arr(arr, count));
system("pause");
return 0;
}
5.
fast 走两个 slow走一个
相遇后
slow 走一个 fast走一个 再次相遇
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int Find_Duplicate_Ring(int *arr, int size)
{
assert(arr != NULL);
int slow = 0;
int fast = 0;
do
{
slow = arr[slow];
fast = arr[arr[fast]];
} while (slow != fast);
slow = 0;
while (slow != fast)
{
slow = arr[slow];
fast = arr[fast];
}
return slow;
}
int main()
{
int arr[] = { 5, 3, 1, 2, 3, 4 };
int count = sizeof(arr) / sizeof(arr[0]);
printf("%d ", Find_Duplicate_Ring(arr, count));
system("pause");
return 0;
}