Assign Cookies Problem 题目太长了 Solution 第一种思路是遍历孩子的那个数组 1234567891011121314151617181920212223242526 public class { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int count = 0; int j = 0; for(int i = 0; i < g.length; i++) { while(j < s.length && g[i] > s[j]){ j++; } if( j < s.length) { count++; j++; } else { break; } } return count; }} 第二种思路是遍历饼干数组 12345678910111213141516171819 public class { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int i = 0; for(int j = 0; i < g.length && j < s.length; j++) { // 以一块饼干,去匹配孩子 // 如果当前这个孩子可以匹配,那下一轮可以匹配下一个孩子(i++) // 不匹配的话,到下一轮循环去看,下一块饼干去匹配 if(g[i] <= s[j]){ i++; } } return i; }} 赞微海报分享
近期评论