IT常识
技术 Python PHP JavaScript IOS Android Java 数据库 资源 公众号 代码片段 github
  • IT常识
  • 技术

LintCode题解之最长单词

Posted 2020-10-15

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LintCode题解之最长单词相关的知识,希望对你有一定的参考价值。

 

 

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

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题解之最长单词的主要内容,如果未能解决你的问题,请参考以下文章

LintCode 133. 最长单词

Lintcode53 Reverse Words in a String solution 题解

《LeetCode之每日一题》:149.通过删除字母匹配到字典里最长单词

LintCode题解之比较字符串

LintCode题解之统计数字

LintCode题解之斐波纳契数列

(c)2006-2024 SYSTEM All Rights Reserved IT常识