注意两边size大小不同可能导致的问题即可,简单的贪心算法
1 public class Solution { 2 public int findContentChildren(int[] g, int[] s) { 3 Arrays.sort(g); 4 Arrays.sort(s); 5 int ans = 0; 6 int gIndex = 0; 7 for (int i = 0; i < s.length; i++) { 8 if (s[i] >= g[gIndex]) { 9 ans++; 10 gIndex++; 11 if (gIndex == g.length) return gIndex; 12 } 13 } 14 return ans; 15 } 16 }