mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加0455.分发饼干java代码
This commit is contained in:
@ -117,6 +117,7 @@ public:
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
|
||||
public int findContentChildren(int[] g, int[] s) {
|
||||
Arrays.sort(g);
|
||||
Arrays.sort(s);
|
||||
@ -132,6 +133,25 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
```java
|
||||
class Solution {
|
||||
// 思路2:优先考虑胃口,先喂饱大胃口
|
||||
public int findContentChildren(int[] g, int[] s) {
|
||||
Arrays.sort(g);
|
||||
Arrays.sort(s);
|
||||
int count = 0;
|
||||
int start = s.length - 1;
|
||||
// 遍历胃口
|
||||
for (int index = g.length - 1; index >= 0; index--) {
|
||||
if(start >= 0 && g[index] <= s[start]) {
|
||||
start--;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
|
Reference in New Issue
Block a user