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

Code Signal_练习题_All Longest Strings

时间:2018-07-22 00:23:41      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:key   def   size   empty   closed   tar   inpu   onclick   code   

Given an array of strings, return another array containing all of its longest strings.

Example

For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

Input/Output

    • [execution time limit] 4 seconds (py3)

    • [input] array.string inputArray

      A non-empty array.

      Guaranteed constraints:
      1 ≤ inputArray.length ≤ 10,
      1 ≤ inputArray[i].length ≤ 10.

    • [output] array.string

      Array of the longest strings, stored in the same order as in the inputArray.

 

 

我的解答:

1 def allLongestStrings(inputArray):
2     li = []
3     m = max(inputArray,key=len)
4     for i in inputArray:
5         if len(i) == len(m):
6             li.append(i)
7     return li

 

膜拜大佬:

技术分享图片
1 def allLongestStrings(inputArray):
2     m = max(len(s) for s in inputArray)
3     r = [s for s in inputArray if len(s) == m]
4     return r
View Code

 

虽然代码比大佬写的多,但至少想法一样了....

 

Code Signal_练习题_All Longest Strings

标签:key   def   size   empty   closed   tar   inpu   onclick   code   

原文地址:https://www.cnblogs.com/YD2018/p/9348437.html

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