给一个词典,找出其中所有最长的单词。
样例
在词典
{
"dog",
"google",
"facebook",
"internationalization",
"blabla"
}
中, 最长的单词集合为 ["internationalization"]
在词典
{
"like",
"love",
"hate",
"yes"
}
中,最长的单词集合为 ["like", "love", "hate"]
class Solution { public: /* * @param dictionary: an array of strings * @return: an arraylist of strings */ vector<string> longestWords(vector<string> &d) { // write your code here vector<string> res; int sz=d.size(); if(sz==0) { return res; } int max=d[0].size(); res.push_back(d[0]); for(int i=1;i<sz;i++) { int strSz=d[i].size(); if(max<strSz) { max=strSz; res.clear(); res.push_back(d[i]); } else if(max==strSz) { res.push_back(d[i]); } } return res; } };