LintCode解题思路已阅LintCode解题思路

OJ lintcode 最长单词

2017-02-19  本文已影响6人  DayDayUpppppp

给一个词典,找出其中所有最长的单词。
您在真实的面试中是否遇到过这个题?
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;
    }
};

上一篇 下一篇

猜你喜欢

热点阅读