leetcode 524. Longest Word in Dictionary through Deleting (自定义sort排序cmp)

来源:互联网 发布:ipad编程程序 编辑:程序博客网 时间:2024/06/01 09:21

这题没什么难度,主要训练自定义sort排序熟练程度,还有双指针的熟练程度。

class Solution {public:    static bool cmp(const string &a, const string & b) {        if (a.size()!=b.size()) {            return a.size()>b.size();        }        return a<b;    }    int check(string &s, string &word) {        int i=0, j=0;        for (;i<word.size()&&j<s.size();) {            if (word[i]==s[j]) {                ++i;            }            ++j;        }        if (i>=word.size()) {            return i;        }        return -1;    }    string findLongestWord(string s, vector<string>& d) {        sort(d.begin(), d.end(), Solution::cmp);        int max_len = 0;        string res = "";        for (int i=0;i<d.size();++i) {            int len = check(s, d[i]);            if (len > max_len) {                max_len = len;                res = d[i];            }        }        return res;    }};


阅读全文
0 0
原创粉丝点击