【每日刷题】Day71
🥕个人主页:开敲🍉
🔥所属专栏:每日刷题🍍
🌼文章目录🌼
1. 牛群分隔
2. 912. 排序数组 - 力扣(LeetCode)
3. 791. 自定义字符串排序 - 力扣(LeetCode)
1. 牛群分隔
//思路:归并。将<x的值和≥x的值分别存入两个数组中。随后先将<x的值放回链表,后将≥x的值放回链表。
typedef struct ListNode LN;
struct ListNode* cow_partition(struct ListNode* head, int x )
{
int arr1[101] = {0};
int count1 = 0;
int arr2[101] = {0};
int count2 = 0;
LN* pmove = head;
while(pmove)
{
if(pmove->val>=x)
arr2[count2++] = pmove->val;//存大
else
arr1[count1++] = pmove->val;//存小
pmove = pmove->next;
}
pmove = head;
int flag1 = 0;
int flag2 = 0;
while(flag1<count1)
{
pmove->val = arr1[flag1++];//放小
pmove = pmove->next;
}
while(flag2<count2)
{
pmove->val = arr2[flag2++];//放大
pmove = pmove->next;
}
return head;
}
2. 912. 排序数组 - 力扣(LeetCode)
//思路①:记数排序void CountSort(int* nums,int size)
{
int max = 0;
int min = 0;
for(int i = 0;i<size;i++)
{
if(nums[i]>nums[max])
max = i;
if(nums[i]<nums[min])
min = i;
}
int x = nums[max];
int y = nums[min];
int* hash = (int*)calloc(x-y+1,sizeof(int));
for(int i = 0;i<size;i++)
{
hash[nums[i]-y]+=1;
}
int count = 0;
for(int i = 0;i<=x-y;i++)
{
while(hash[i])
{
nums[count++] = i+y;
hash[i]--;
}
}
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
CountSort(nums,numsSize);
*returnSize = numsSize;
return nums;
}
//思路②:堆排序
//交换函数
void Swap(int* x,int* y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
//向下调整建堆
void AdjustDown(int* arr,int parents,int size)
{
int child = parents*2+1;
while(child<size)
{
if(child+1<size&&arr[child+1]>arr[child])
child++;
if(arr[child]>arr[parents])
Swap(&arr[child],&arr[parents]);
else
break;
parents = child;
child = parents*2+1;
}
}
//堆排序
void HeapSort(int* arr,int size)
{
for(int i = (size-2)/2;i>=0;i--)
{
AdjustDown(arr,i,size);
}
while(size)
{
Swap(&arr[0],&arr[size-1]);
size--;
AdjustDown(arr,0,size);
}
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
HeapSort(nums,numsSize);
*returnSize = numsSize;
return nums;
}
//思路③:希尔排序
//插入排序
void InsertSort(int* arr,int size)
{
for(int i = 0;i<size-1;i++)
{
int end = i+1;
int tmp = arr[end];
while(end-1>=0)
{
if(arr[end-1]>tmp)
arr[end] = arr[end-1];
else
break;
end--;
}
arr[end] = tmp;
}
}
//希尔排序
void ShellSort(int* arr,int size)
{
int gap = size;
while(gap>1)
{
gap = gap/3+1;
for(int i = 0;i<size-gap;i++)
{
int end = i+gap;
int tmp = arr[end];
while(end-gap>=0)
{
if(arr[end-gap]>tmp)
arr[end] = arr[end-gap];
else
break;
end-=gap;
}
arr[end] = tmp;
}
}
InsertSort(arr,size);
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
ShellSort(nums,numsSize);
*returnSize = numsSize;
return nums;
}
//思路④:归并排序
//归并排序子函数
void _MergeSort(int* arr,int* tmp,int left,int right)
{
if(left>=right)
return;
int mid = (left+right)/2;
_MergeSort(arr,tmp,left,mid);
_MergeSort(arr,tmp,mid+1,right);
int begin1 = left;
int end1 = mid;
int begin2 = mid+1;
int end2 = right;
int count = 0;
while(begin1<=end1&&begin2<=end2)
{
if(arr[begin1]<=arr[begin2])
tmp[count++] = arr[begin1++];
else
tmp[count++] = arr[begin2++];
}
while(begin1<=end1)
{
tmp[count++] = arr[begin1++];
}
while(begin2<=end2)
{
tmp[count++] = arr[begin2++];
}
memcpy(arr+left,tmp,sizeof(int)*count);
}
//归并排序
void MergeSort(int* arr,int size)
{
int* tmp = (int*)malloc(sizeof(int)*(size+1));
_MergeSort(arr,tmp,0,size);
free(tmp);
tmp = NULL;
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
MergeSort(nums,numsSize-1);
*returnSize = numsSize;
return nums;
}
//思路⑤:归并排序(非递归)
//归并排序 (非递归)
void MergeSortNoRe(int* arr, int size)
{
int* tmp = (int*)malloc(sizeof(int) * size);
int gap = 1;
while (gap < size)
{
for (int i = 0; i < size; i += 2 * gap)
{
int count = 0;
int begin1 = i;
int end1 = i + gap - 1;
int begin2 = i + gap;
int end2 = i + 2 * gap - 1;
if (begin2 >= size)
break;
if (end2 >= size)
end2 = size - 1;
while (begin1 <= end1 && begin2 <= end2)
{
if (arr[begin1] < arr[begin2])
tmp[count++] = arr[begin1++];
else
tmp[count++] = arr[begin2++];
}
while (begin1 <= end1)
{
tmp[count++] = arr[begin1++];
}
while (begin2 <= end2)
{
tmp[count++] = arr[begin2++];
}
memcpy(arr + i, tmp, sizeof(int) * (end2 - i + 1));
}
gap *= 2;
}
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
MergeSortNoRe(nums,numsSize);
*returnSize = numsSize;
return nums;
}
3. 791. 自定义字符串排序 - 力扣(LeetCode)
//思路:哈希。记录s字符串中每个字母的个数,根据order字符串字母出现的顺序将字母放入答案数组中。
char* customSortString(char* order, char* s)
{
char* ans = (char*)malloc(sizeof(char)*201);
int count = 0;
int hash[27] = {0};
for(int i = 0;i<strlen(s);i++)
{
hash[s[i]-'a']+=1;//统计个数
}
for(int i = 0;i<strlen(order);i++)
{
while(hash[order[i]-'a'])
{
ans[count++] = order[i];//判断s中是否有这个字母以及有多少个,再根据order字符串中字母出现的顺序放入答案数组
hash[order[i]-'a']--;
}
}
for(int i = 0;i<strlen(s);i++)
{
while(hash[s[i]-'a'])//将s中剩余字母放进答案数组
{
ans[count++] = s[i];
hash[s[i]-'a']--;
}
}
ans[count] = '\0';//最后别忘了加上'\0'
return ans;
}