首页 > 其他 > 详细

leetcode:longest words

时间:2016-03-21 18:21:27      阅读:137      评论:0      收藏:0      [点我收藏+]

1、

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

Given

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

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

2、

  思路:

  1、得到数组里面最长的字符串大小

  2、判断相等的字符串大小,添加进去。

3、源码:

  

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

 

leetcode:longest words

原文:http://www.cnblogs.com/zilanghuo/p/5302865.html

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