码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode题解之最长单词

时间:2017-11-26 11:16:30      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:ges   zh-cn   ==   min   修改   www   java   blog   word   

技术分享图片 

技术分享图片

 

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

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题解之最长单词

标签:ges   zh-cn   ==   min   修改   www   java   blog   word   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!