[LintCode] Longest Words
ArrayList<String> longestWords(String[] dictionary) { // write your code here ArrayList<String> res = new ArrayList<String>(); if(dictionary == null || dictionary.length == 0) return res; int len = dictionary[0].length(); for(int i = 0; i < dictionary.length; i++) { if(dictionary[i].length() == len) res.add(dictionary[i]); else if(dictionary[i].length() > len){ len = dictionary[i].length(); res.clear(); res.add(dictionary[i]); } } return res; }
Leave A Comment