Code Signal_练习题_All Longest Strings

Posted yd2018

tags:

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

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

Code Signal_练习题_arrayChange

Code Signal_练习题_Minesweeper

Code Signal_练习题_stringsRearrangement

Code Signal_练习题_firstDigit

Code Signal_练习题_digitDegree

Code Signal_练习题_matrixElementsSum