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.

我的解答:

 def allLongestStrings(inputArray):
li = []
m = max(inputArray,key=len)
for i in inputArray:
if len(i) == len(m):
li.append(i)
return li

膜拜大佬:

 def allLongestStrings(inputArray):
m = max(len(s) for s in inputArray)
r = [s for s in inputArray if len(s) == m]
return r

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

Code Signal_练习题_All Longest Strings的更多相关文章

  1. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  2. Code Signal_练习题_reverseParentheses

    You have a string s that consists of English letters, punctuation marks, whitespace characters, and ...

  3. Code Signal_练习题_commonCharacterCount

    Given two strings, find the number of common characters between them. Example For s1 = "aabcc&q ...

  4. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  5. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  6. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  7. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  8. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  9. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

随机推荐

  1. Reservoir Sampling-382. Linked List Random Node

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  2. Topological Sor-207. Course Schedule

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  3. (2)特征点匹配,并求旋转矩阵R和位移向量t

    include头文件中有slamBase.h # pragma once // 各种头文件 // C++标准库 #include <fstream> #include <vector ...

  4. about BFC

    https://www.cnblogs.com/lhb25/p/inside-block-formatting-ontext.html Box.Formatting Context(BFC)

  5. chrome浏览器本地文件支持ajax请求的解决方法

    .右键chrome的快捷键--->点击属性 .在快捷方式的选项卡的目标里末尾填上如下内容: --allow-file-access-from-files 要关闭所有的chrome打开的网页

  6. centos6下无法使用lsof命令"-bash: lsof: command not found"

    1. 问题描述在centos下, 无法使用命令lsof, 出现以下信息:# lsof -i:3690-bash: lsof: command not found2. 解决方法我们可以通过yum来安装: ...

  7. Sql Sever 2012数据库更名失败 error:5030

    打开SQL Server 2012数据库时,对HzWind_2MW数据库名修改为HzWind_2MW_GEM,结果出现如图1所示: 图1 数据库更名时出现error:5030 数据库不能重名名5030 ...

  8. rabbitmq系列一 之简单队列

    1. rabbitmq简介 rabbitmq是一个消息代理,或者讲是一个消息中间件.主要是用来接收和转发信息的,它是对消息不做任何处理的.MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写 ...

  9. C、C++基础和编程风格 (转)

    原文链接 作者:寒小阳时间:2013年8月.出处:http://blog.csdn.net/han_xiaoyang/article/details/10515417.声明:版权所有,转载请注明出处, ...

  10. spring boot日志管理配置

    spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J,Log4J2和Logback.每种L ...