"LeetCode Zero Basic Guide" (6th Lecture) C Sorting API
Uploading...Reupload CancelShen Xiaoyu2021-12-05 19:49:02
Today's question further deepens my understanding of arrays and pointers
Above
1. Sort array
-
int cmp(const void*a, const void*b){
-
return *(int *)a - *(int *)b;
-
}
-
int* sortArray(int* nums, int numsSize, int* returnSize){
-
qsort(nums, numsSize, sizeof(int), cmp);
-
* returnSize = numsSize;
-
return nums;
-
}
Second, the majority of elements (pay attention to the range of int type)
int cmp(const void*a, const void*b){
return *(unsigned int *)b - *(unsigned int *)a;
}
int majorityElement(int* nums, int numsSize){
qsort(nums, numsSize, sizeof(int), cmp);
return nums[numsSize / 2];
}
3. There are repeated elements
-
int cmp(const void*a, const void*b){
-
return *(int *)a - *(int *)b;
-
}
-
bool containsDuplicate(int* nums, int numsSize){
-
qsort(nums, numsSize, sizeof(int), cmp);
-
int i;
-
for(i = 0; i < (numsSize - 1); ++i){
-
if(nums[i] == nums[i + 1]){
-
return true;
-
}
-
}
-
return false;
-
}
4. Maximum distance
int cmp(const void*a, const void*b){
return *(int *)a - *(int *)b;
}
int maximumGap(int* nums, int numsSize){
int i = 0;
qsort(nums, numsSize, sizeof(int), cmp);
if(numsSize < 2){
return 0;
}
int max = (nums[i + 1] - nums[i]);
for(i =1; i < (numsSize - 1); ++i){
if( max < (nums[i + 1] - nums[i]) ){
max = nums[i + 1] - nums[i];
}
}
return max;
}
5. Sort array by parity
Sixth, the minimum time difference (this does not really look at the analysis)
-
int cmp(const void*a, const void*b){
-
return *(int *)a - *(int *)b;
-
}
-
int min(int a, int b){
-
return (a > b) ? b : a;
-
}
-
int findMinDifference(char ** timePoints, int timePointsSize){
-
int *ret = (int *)malloc(sizeof(int) * timePointsSize);
-
int m , n;
-
int i, ans = 1440;
-
for(i = 0; i < timePointsSize; ++i){
-
sscanf(timePoints[i], "%d:%d", &m, &n);
-
ret[i] = m * 60 + n;
-
}
-
qsort(ret, timePointsSize, sizeof(int), cmp);
-
for(i = 1; i < timePointsSize; ++i){
-
ans = min(ans, ret[i] - ret[i - 1]);
-
}
-
ans = min(ans, ret[0] - ret[timePointsSize - 1] + 1440);
-
return ans;
-
}
7. Maximum perimeter of a triangle
int cmp(const void*a, const void*b){
return *(int *)a - *(int *)b;
}
int largestPerimeter(int* nums, int numsSize){
int i;
qsort(nums, numsSize, sizeof(int), cmp);
for(i = numsSize - 1; i >= 2; --i){
if(nums[i - 1] + nums[i - 2] > nums[i]){
return (nums[i] + nums[i - 1] + nums[i - 2]);
}
}
return 0;
}
8. Lifeboats
int cmp(const void*a, const void*b){
return *(int *)a - *(int *)b;
}
int numRescueBoats(int* people, int peopleSize, int limit){
int years = 0;
int m = 0, k = peopleSize - 1;//people[m] represents the weight of the lightest person left, people[k] the heaviest person
qsort(people, peopleSize, sizeof(int), cmp);
while(k >= m){
if(k == m){
++years;
break;
}
else if(people[m] + people[k] <= limit){
++years;
++m;
k--;
}
else{
++years;
--k;
}
}
return ans;
}
We must prevent the array from out of bounds! ! !
Summarize
Today's inscription took 4 or 5 hours, but it was worth it. I learned a lot and I am happy.
When brushing Leetcode, if there is a problem, don't panic. Just like what Brother Hero said in the video, create more test cases by yourself, and then find the direction.
Finally, I hope you will pay more attention to Brother Hero (manual dog head)