先排序,再遍历数组:
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int ans=0;
for(int i=0,j=0;i<g.size()&&j<s.size();)
{
if(g[i]<=s[j])
{
ans++;
i++;
j++;
}
else
j++;
}
return ans;
}
};
本文介绍了一种通过先排序后遍历数组的方式解决分配饼干问题的方法。该方法使用C++实现,具体步骤包括:首先对儿童的胃口和饼干大小进行排序,然后遍历数组匹配每个儿童能获得的最大尺寸饼干,从而得出最终能够满足条件的儿童数量。
361

被折叠的 条评论
为什么被折叠?



