524. Longest Word in Dictionary through Deleting

来源:互联网 发布:tp框架数据库修改语句 编辑:程序博客网 时间:2024/05/17 01:46

简单题,关键在于题意的理解。lexicographical order指的是按照字母顺序排序。

class Solution {public:    static bool cmp(string& a,string& b)    {        if(a.length()>b.length())            return true;        else if(a.length()==b.length())        {            if(a.compare(b)<0)                return true;            else                return false;        }        else            return false;    }    bool judge(string strShort,string strLong)    {        if(strLong.length()<strShort.length())            return false;        else        {            int index1=0;            int index2=0;            while(index1<strShort.length()&&index2<strLong.length())            {                if(strShort[index1]==strLong[index2])                {                    index1++;                    index2++;                }                else                    index2++;            }            if(index1==strShort.length())                return true;            else                return false;        }    }    string findLongestWord(string s, vector<string>& d) {        sort(d.begin(),d.end(),cmp);        for(int i=0;i<d.size();i++)        {            if(judge(d[i],s)==true)                return d[i];        }        return "";    }};
0 0
原创粉丝点击