码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode: Longest Words

时间:2015-11-27 06:40:42      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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

 

LintCode: Longest Words

标签:

原文地址:http://www.cnblogs.com/CheeseZH/p/4999501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!