首页 > 其他 > 详细

双指针leetcode

时间:2021-08-30 03:44:04      阅读:6      评论:0      收藏:0      [点我收藏+]

455. 分发饼干

贪心算法求解,孩子指针和饼干指针,满足条件的话两个指针都++,不满足只有饼干++。

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

  

双指针leetcode

原文:https://www.cnblogs.com/Jessicax/p/15202731.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!