您的位置:首页 > 其它

LeetCode: Assign Cookies

2016-11-21 11:04 435 查看
注意两边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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: