update 455.分發餅乾 C語言

This commit is contained in:
李昂謙
2022-09-30 14:48:18 +08:00
parent 180c08fc46
commit 2772fd498d

View File

@ -274,6 +274,7 @@ function findContentChildren(g: number[], s: number[]): number {
### C
```c
///小餅乾先餵飽小胃口的
int cmp(int* a, int* b) {
return *a - *b;
}
@ -296,6 +297,33 @@ int findContentChildren(int* g, int gSize, int* s, int sSize){
}
```
```c
///大餅乾先餵飽大胃口的
int cmp(int* a, int* b) {
return *a - *b;
}
int findContentChildren(int* g, int gSize, int* s, int sSize){
if(sSize == 0)
return 0;
//将两个数组排序为升序
qsort(g, gSize, sizeof(int), cmp);
qsort(s, sSize, sizeof(int), cmp);
int count = 0;
int start = sSize - 1;
for(int i = gSize - 1; i >= 0; i--) {
if(start >= 0 && s[start] >= g[i] ) {
start--;
count++;
}
}
return count;
}
```
### Scala
```scala