在词典
中, 最长的单词集合为
{
"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;
}
};
来源: http://www.bubuko.com/infodetail-2477828.html