添加0455.分发饼干java代码

This commit is contained in:
ironartisan
2021-08-26 20:04:48 +08:00
parent c8149ced78
commit f93ad90c69

View File

@ -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