美文网首页
455. Assign Cookies

455. Assign Cookies

作者: 殷水臣 | 来源:发表于2017-02-22 18:03 被阅读0次

这道题也没啥好说的,自己的代码跑得比较快可能是因为用的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;
    }
}

相关文章

网友评论

      本文标题:455. Assign Cookies

      本文链接:https://www.haomeiwen.com/subject/zmmmwttx.html