您的位置:首页 > 其它

[Lintcode]Longest Words

2016-02-17 05:37 441 查看
Given a dictionary, find all of the longest words in the dictionary.(do
it in one pass)

简单题,直接写代码

class Solution {
/**
* @param dictionary: an array of strings
* @return: an arraylist of strings
*/
ArrayList<String> longestWords(String[] dictionary) {
ArrayList<String> res = new ArrayList<String>();

for(String str : dictionary) {
if(res.isEmpty() || str.length() == res.get(0).length()) {
res.add(str);
}
else if(res.isEmpty() || str.length() > res.get(0).length()) {
res.clear();
res.add(str);
}
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lintcode