LintCode: Longest Words
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 };
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。