mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
update 455.分發餅乾 C語言
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user