C++
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 if (dictionary.size() <= 1) { 10 return dictionary; 11 } 12 13 vector<string> result; 14 int max_len = 0, cur_len; 15 16 for (int i=0; i<dictionary.size(); i++) { 17 cur_len = dictionary[i].size(); 18 if(cur_len < max_len) { 19 continue; 20 } 21 if(cur_len > max_len) { 22 result.clear(); 23 max_len = cur_len; 24 } 25 result.push_back(dictionary[i]); 26 } 27 return result; 28 } 29 };