LintCode 题目:最长单词

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;

即可:


 

发布了303 篇原创文章 · 获赞 550 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_42410605/article/details/103206514