[LintCode] 最长单词

 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 };

 

posted @ 2015-06-30 00:03  jianchao-li  阅读(403)  评论(0编辑  收藏  举报