
public static int findContentChildren(int[] g, int[] s) {
int count = 0;
for(int i=g.length-1,j=s.length-1;i>=0&&j>=0;--i)
if(g[i]<=s[j]){
--j;
++count;
}
return count;
}
public static int findContentChildren1(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int gLen = g.length;
int sLen = s.length;
int gStart = 0;
int sStart = 0;
while (gStart<gLen && sStart <sLen){
if(g[gStart]<=s[sStart]){
gStart++;
sStart++;
gStart++;
}else {
sStart++;
}
}
return gStart;
}
本文探讨了分配饼干问题的两种解决方案。一种是通过遍历孩子和饼干数组,另一种则是先对数组进行排序,然后使用双指针技巧来解决。通过对这两种方法的对比,可以更好地理解如何高效地解决问题。
2357

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



