lintcode-easy-Longest Words

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-easy-Longest Words相关的知识,希望对你有一定的参考价值。

Given a dictionary, find all of the longest words in the dictionary.

Example

Given

{
  "dog",
  "google",
  "facebook",
  "internationalization",
  "blabla"
}

the longest words are(is) ["internationalization"].

Given

{
  "like",
  "love",
  "hate",
  "yes"
}

the longest words are ["like", "love", "hate"].

Challenge

It‘s easy to solve it in two passes, can you do it in one pass?

 

class Solution {
    /**
     * @param dictionary: an array of strings
     * @return: an arraylist of strings
     */
    ArrayList<String> longestWords(String[] dictionary) {
        // write your code here
        ArrayList<String> result = new ArrayList<String>();
        
        if(dictionary == null || dictionary.length == 0)
            return result;
        
        for(int i = 0; i < dictionary.length; i++){
            if(result.size() == 0)
                result.add(dictionary[i]);
            else{
                if(dictionary[i].length() > result.get(result.size() - 1).length()){
                    result = new ArrayList<String>();
                    result.add(dictionary[i]);
                }
                else if(dictionary[i].length() == result.get(result.size() - 1).length()){
                    result.add(dictionary[i]);
                }
            }
        }
        
        return result;
    }
};

 

以上是关于lintcode-easy-Longest Words的主要内容,如果未能解决你的问题,请参考以下文章

wor20161202

CV项目调试CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT问题

markdown CSS_media_query_max_width_not_working

如何使用 API 创建跨 TFS 集合的 TFS/Azure DevOps 工作项查询并将其导出到 Wor/Excel/Outlook 等

如何通过java将多个word文档合成一个wor

Administrator privileges required for OLE Remote Procedure Call debugging: this feature will not wor