Longest Words
http://www.lintcode.com/en/problem/longest-words/
Solution
比较简单,关键在怎么one pass。
class Solution {
public:
/**
* @param dictionary: a vector of strings
* @return: a vector of strings
*/
vector<string> longestWords(vector<string> &dictionary) {
// write your code here
vector<string> result;
int maxlen = 0;
for(int i=0; i<dictionary.size(); i++) {
string str = dictionary[i];
if(str.length() == maxlen) {
result.push_back(str);
} else if (str.length() > maxlen) {
result.clear();
result.push_back(str);
maxlen = str.length();
}
}
return result;
}
};