LintCode 133--- 最长单词

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-30 14:49   34   0
public class Solution {
    /*
     * @param dictionary: an array of strings
     * @return: an arraylist of strings
     */
    public List<String> longestWords(String[] dictionary) {//判断是否为空,直接返回
    if(dictionary==null||dictionary.length==0)return null;
     //创建返回集合
    List<String> list=new ArrayList<String>();
    //设定第一个为最大长度
    int max=dictionary[0].length();
    //遍历
        for(int a=0;a<dictionary.length;a++){
    //如果后面的数大于max,max改变,集合全部清除,在添加当前字符串
        if(max<dictionary[a].length()){
        max=dictionary[a].length();
        list.clear();
        list.add(dictionary[a]);
    //如果等于直接添加当前字符串
        }else if(max==dictionary[a].length()){
        list.add(dictionary[a]);
        }
    }
    return list;
    }
}    

转载于:https://www.cnblogs.com/cnmoti/p/10828493.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP