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

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

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. ActiveMQ使用线程池实现消息的生产与消费

    jar文件:spring3.1jar,以及 项目src路径下文件:config.properties 读取config.properties文件JAVA类: package com.lejob.lej ...

  2. 修正eth0,解决虚拟机桥接问题

    centos 中没有 ifcfg-eth0 配置文件的解决办法 1.也就是说是centos6改用NetworkManager方式管理网络了,可以运行如下命令进行确认: chkconfig --list ...

  3. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...

  4. 【8】JMicro微服务-JMicro ZKUI

    ZKUI是一个开源项目,是一个查看,修改ZK数据非常方便的工具.JMicro基于ZK做服务治理,配置管理,因此使用ZKUI会提供非常大的方便. Github地址:https://github.com/ ...

  5. 【NOIP2017】逛公园 最短路+DP

    诶,去年场上不会处理$0$的环,只拿了$60$有点可惜. 我们先不管边边权为$0$的边. 我们先跑一次最短路,令$dis[u]$表示从$1$至$u$的最短路的长度. 那么根据题目的要求,从起点走到$u ...

  6. POJ 2336

    #include <iostream> using namespace std; int main() { //freopen("acm.acm","r&qu ...

  7. PL/SQL DEVELOPER数字超长显示了科学计数法

    问题: 最近在做项目中,ID使用了长整形,10进制数值大约长度17位,在pl/sql developer 上数值由科学计数法显示. 在查看时不是很方便,且数值进行了省略显示,不准确. 解决方法: 在t ...

  8. jython实现java运行python代码

    Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现.最近的一个项目需要将python代码转换成java实现,所以用了一 ...

  9. 【树】Count Complete Tree Nodes

    题目: 求完全二叉树节点数. 思路: 满二叉树的节点数是2^k-1,k是树的深度. 所以我们可以先判断该树是否为满二叉树,然后是的话直接返回结果,如果不是递归地求解子树. 这样不用遍历所有的节点.复杂 ...

  10. 可视化的Redis数据库管理工具redis-desktop-manager的初步使用(图文详解)

    不多说,直接上干货! 无论是Linux 还是 Windows里安装Redis, Windows里如何正确安装Redis以服务运行(博主推荐)(图文详解) Windows下如何正确下载并安装可视化的Re ...