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. 程序猿的日常——Java基础之抽象类与接口、枚举、泛型

    再次回顾这些基础内容,发现自己理解的又多了一点.对于一些之前很模糊的概念,渐渐的清晰起来. 抽象类与接口 抽象类通常是描述一些对象的通用方法和属性,并且默认实现一些功能,它不能被实例化.接口仅仅是描述 ...

  2. 基于SWOOLE的分布式SOCKET消息服务器架构

    消息服务器使用socket,为避免服务器过载,单台只允许500个socket连接,当一台不够的时候,扩充消息服务器是必然,问题来了,如何让链接在不同消息服务器上的用户可以实现消息发送呢? 要实现消息互 ...

  3. 00-python概述。

    人生苦短,我用Python. -发展历史: - 1989年,由Guido van Rossum开始开发, - 1991年,发布第一个公开发行版,第一个Python编译器(同时也是解释器)诞生. - 2 ...

  4. Go语言学习笔记(4)——数组和切片

    1 数组的特点: 长度固定.元素数据类型相同.下标从0开始 1.1 声明和初始化: var array_name [size] type         var arr1 [10] float32   ...

  5. 【learning】vim爆改记 (如何让vim用起来像devc++)

    由于本蒟蒻NOIP人品大爆发,能去冬令营,故准备开始练习使用linux下的IDE:vim 在dalao DTZ的帮助下,我装好了vim,并做了最初的配置. 然而........好难用啊,怎么和devc ...

  6. SLAP(Speaker-Listener Label Propagation Algorithm)社区发现算法

    其中部分转载的社区发现SLPA算法文章 一.概念 社区(community)定义:同一社区内的节点与节点之间关系紧密,而社区与社区之间的关系稀疏. 设图G=G(V,E),所谓社区发现是指在图G中确定n ...

  7. UR5 改造

    背景 项目要求 UR5 安装在移动平台上,需要做以下改造: 供电:由 220V 市电改为直流电源(锂电池)直接供电: 控制:由示教器控制改为上位机远程控制 线路改造 1. UR5 电气图(来自UR5服 ...

  8. Ubuntu安装PhpStorm并设置快速启动phpstorm

    使用sudo apt-get install phpstorm 安装php后,没有在桌面生成phpstorm的快捷方式,如果将phpstorm.sh的链接放到/usr/local/bin ,虽然可以从 ...

  9. linux 手动释放buff/cache

    为了解决buff/cache占用过多的问题执行以下命令即可 syncecho 1 > /proc/sys/vm/drop_cachesecho 2 > /proc/sys/vm/drop_ ...

  10. (转)Python标准库:内置函数print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

    原文:https://blog.csdn.net/caimouse/article/details/44133241 https://www.cnblogs.com/owasp/p/5372476.h ...