首页 > 学院 > 开发设计 > 正文

524. Longest Word in Dictionary through Deleting

2019-11-06 06:54:14
字体:
来源:转载
供稿:网友

简单题,关键在于题意的理解。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 ""; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表