[LintCode]最長単語

3040 ワード

 1 class Solution {
 2 public:
 3     /**
 4      * @param dictionary: a vector of strings
 5      * @return: a vector of strings
 6      */
 7     vector<string> longestWords(vector<string> &dictionary) {
 8         // write your code here
 9         vector<string> res;
10         int len = 0;
11         for (int i = 0; i < (int)dictionary.size(); i++) {
12             if (dictionary[i].length()>= len) {
13                 if (dictionary[i].length() > len) {
14                     res.clear();
15                     len = dictionary[i].length();
16                 }
17                 res.push_back(dictionary[i]);
18             }
19         }
20         return res;
21     }
22 };