LintCode 题目:最长单词
程序员文章站
2022-03-25 08:12:01
...
URL:https://www.lintcode.com/problem/longest-word/description
描述
给一个词典,找出其中所有最长的单词。
样例
样例 1:
输入: {
"dog",
"google",
"facebook",
"internationalization",
"blabla"
}
输出: ["internationalization"]
样例 2:
输入: {
"like",
"love",
"hate",
"yes"
}
输出: ["like", "love", "hate"]
在代码段中添加:
vector<string> lcc;
int count = dictionary[0].size();
lcc.push_back(dictionary[0]);
for (int i = 1; i < dictionary.size(); i++) {
/* code */
if(dictionary[i].size()>count){
lcc.clear();
lcc.push_back(dictionary[i]);
count = dictionary[i].size();
}else if(dictionary[i].size()==count){
lcc.push_back(dictionary[i]);
}
}
return lcc;
即可:
上一篇: Bugku_字符?正则?