OJ lintcode 最长单词

给一个词典,找出其中所有最长的单词。
您在真实的面试中是否遇到过这个题?
Yes
样例
在词典
{
“dog”,
“google”,
“facebook”,
“internationalization”,
“blabla”
}
中, 最长的单词集合为 [“internationalization”]
在词典
{
“like”,
“love”,
“hate”,
“yes”
}
中,最长的单词集合为 [“like”, “love”, “hate”]

class Solution {
public:
    /**
     * @param dictionary: a vector of strings
     * @return: a vector of strings
     */
    vector<string> longestWords(vector<string> &dictionary) {
        // write your code here
        vector<string> max;

        int temp=0;

        for(int i=0;i<dictionary.size();i++){
            if(temp==dictionary[i].length()){
                max.push_back(dictionary[i]);
            }
            
            if(temp<dictionary[i].length()){
                temp=dictionary[i].length();
                max.clear();
                max.push_back(dictionary[i]);
            }
        

        }

        return max;
    }
};

    原文作者:zhaozhengcoder
    原文地址: https://www.jianshu.com/p/116587cb9e0c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞