首页 > 其他 > 详细

LintCode题解之最长单词

时间:2017-11-26 10:40:58      阅读:230      评论:0      收藏:0      [点我收藏+]

技术分享图片 

技术分享图片

 

这些一次遍历搞定的,套路无非都是在遍历的时候就记录数据的状态,然后根据遍历到的当前的数据的状态来修改最终结果,当遍历完了的时候结果也就确定了。

public class Solution {
    
    /*
     * @param dictionary: an array of strings
     * @return: an arraylist of strings
     */
    public List<String> longestWords(String[] dictionary) {
        
        int maxLength = Integer.MIN_VALUE;
        List<String> resultList = new ArrayList<>();
        
        for(String s : dictionary){
            if(s.length()>maxLength){
                maxLength = s.length();
                resultList = new ArrayList<>();
                resultList.add(s);
            }else if(s.length()==maxLength){
                resultList.add(s);
            }
        }
        
        return resultList;
    }
    
}

 

题目来源: http://www.lintcode.com/zh-cn/problem/longest-words/

 

LintCode题解之最长单词

原文:http://www.cnblogs.com/cc11001100/p/7898183.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!