455. Assign Cookies

这道题也没啥好说的,自己的代码跑得比较快可能是因为用的sort?迷。。。还是需要注意复习一下手写各种排序。

我的解法

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        int output = 0;
        int gNow = 0, sNow = 0;
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        while (sNow != s.size() && gNow != g.size()){
            if (s[sNow] >= g[gNow]){
                ++output;
                ++sNow;
                ++gNow;
            }else{
                ++sNow;
            }
        }
        return output;
    }
};

人家的解法

嗯。。。显然我又做了多余的事。。。要善于利用已有的东西啊。。。

public class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        
        int pointG = 0;
        int pointS = 0;
        
        while (pointG<g.length && pointS<s.length) {
            if (g[pointG]<=s[pointS]) {
                pointG++;
                pointS++;
            } else {
                pointS++;
            }
        }
        
        return pointG;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容