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

 

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

 

转载于:https://www.cnblogs.com/BlameKidd/p/9348437.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值