简单题,关键在于题意的理解。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 ""; }};新闻热点
疑难解答