14. 最长公共前缀

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""

示例 1:

输入: ["flower","flow","flight"]
输出: "fl"

示例 2:

输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。

说明:

所有输入只包含小写字母 a-z 。

我的解答:

class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
res = ""
if not all(strs):
return res
for each in zip(*strs):#zip()函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表
if len(set(each)) == 1:#利用集合创建一个无序不重复元素集
res += each[0]
else:
return res
return res

拓展点:

zip()all()的用法

体会:

内置函数的熟练使用将会大大降低解题的复杂度

LeetCode之旅的更多相关文章

  1. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  2. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  3. [算法学习]开始leetcode之旅

    在此记录一下用javascript刷leetcode的过程,每天都要坚持! 1.Two Sum Given an array of integers, find two numbers such th ...

  4. leetCode之旅(12)-反转二叉树

    背景描述 Homebrew 是 OS X 平台上的包管理工具.用其官网的话说就是: the missing package manager for OS X | OS X 平台遗失的包管理器. 相信在 ...

  5. leetcode之旅(10)-Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  6. leetcode之旅(9)-Reverse Linked List

    题目描述: Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed ei ...

  7. leetcode之旅(8)-Contains Duplicate

    题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...

  8. leetcode之旅(7)-Move Zeroes

    Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...

  9. leetcode之旅(6)-Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  10. leetCode之旅(5)-博弈论中极为经典的尼姆游戏

    题目介绍 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

随机推荐

  1. SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

    2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...

  2. 论文阅读笔记十七:RefineNet: Multi-Path Refinement Networks for High-Resolution Semantic Segmentation(CVPR2017)

    论文源址:https://arxiv.org/abs/1611.06612 tensorflow代码:https://github.com/eragonruan/refinenet-image-seg ...

  3. WCF寄宿IIS

    1.创建一个简单的wcf项目 创建完成后直接运行,结果 然后进行发布 在IIS上新建一个网站,直接进行发布即可 遇到的问题 请求与通配符 mime 映射相匹配.请求映射到静态文件处理程序. 需要注意的 ...

  4. fastjson如何判断JSONObject和JSONArray

    1.fastjson如何判断JSONObject和JSONArray,百度一下,教程还真不少,但是是阿里的fastjson的我是没有找到合适的方法.这里用一个还算可以的方法,算是实现了这个效果. 网上 ...

  5. 一脸懵逼学习Zookeeper(动物园管理员)---》高度可靠的分布式协调服务

    1:Zookeeper是一个分布式协调服务:就是为用户的分布式应用程序提供协调服务 A.zookeeper是为别的分布式程序服务的 B.Zookeeper本身就是一个分布式程序(只要有半数以上节点存活 ...

  6. webpack学习笔记--配置plugins

     Plugin Plugin 用于扩展 Webpack 功能,各种各样的 Plugin 几乎让 Webpack 可以做任何构建相关的事情. 配置 Plugin Plugin 的配置很简单, plugi ...

  7. sharepoint2013 Restore-SPSite 报错,采用数据库还原

    PS C:\Users\spadmin> Restore-SPSite http://hz0xw002049:8099 -Path D:\20170731MossSiteSP.bak -Forc ...

  8. 萨塔尼亚的期末考试(fail)

    题解: 这题比较妙啊... 首先暴力自己算是找不出规律的 有一种直觉就是可以把式子变成{f[1]+...f[n]}+{f[2]+...+f[n]}+{f[3]+...+f[n]}... 然后看了题解发 ...

  9. python全栈开发day86-CRM增删改查 分页

    知识点梳理: 1.分页器保存搜索条件 2.ORM批量插入数据bulk_create 批量创建model实例,在用bulk_create 一次插入数据库 3.自定义不同表的不同字段ModelForm错误 ...

  10. COMException: The data necessary to complete this operation is not yet available.

    问题描述: 最近在公司AE项目中遇到了下面的问题: COMException: The data necessary to complete this operation is not yet ava ...