leetcode 354. Russian Doll Envelopes

You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.

What is the maximum number of envelopes can you Russian doll? (put one inside other)

Note:
Rotation is not allowed.

struct object
{
    bool operator()(pair &a,  pair &b)
    {
        if (a.first == b.first) return a.second > b.second;
        return a.first < b.first;
    }
}myobject;
class Solution {
public:
    typedef bool (Solution::*ltype)(pair,  pair);
    int maxEnvelopes(vector>& envelopes) {
        int ans = 0;
        int size = envelopes.size();
        //lter = &Solution::myfunction;
        sort(envelopes.begin(),envelopes.end(),myobject);
        vector dp;
        for(int i=0;i a,  pair b)
    {
        if (a.first == b.first) return a.second > b.second;
        return a.first < b.first;
    }
    */
};
class Solution {
public:
    typedef bool (Solution::*ltype)(pair,  pair);
    int maxEnvelopes(vector>& envelopes) {
        int ans = 0;
        int size = envelopes.size();
        //lter = &Solution::myfunction;
        sort(envelopes.begin(),envelopes.end(),&Solution::myfunction);
        vector dp;
        for(int i=0;i a,  pair b)
    {
        if (a.first == b.first) return a.second > b.second;
        return a.first < b.first;
    }
    
};

这里想借sort函数讲一下函数指针,类成员函数指针,和函数对象的使用;

//sort函数的第三个参数可以接受函数指针和函数对象,但是不接受类成员函数指针。所以这里我两种都试了一下。但是在使用函数指针的时候出了问题,一开始我定义了一个类成员函数指针变量,将&Solution::myfunction的值赋给这个指针,然后将这个指针变量赋给sort函数的第三个参数,编译错误。应该是这个指针变量的类型是类成员函数指针而不是函数指针的关系,变量类型不匹配。而且要将myfunction函数改成静态成员函数,才能赋给sort函数。后来我直接将&Solution::myfunction的值赋给sort函数的第三个参数,编译就正确了。//

你可能感兴趣的:(leetcode)