Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

flow
flower

flight
 class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if strs==[]:
return ""
shortstr = min(strs,key=len)
for i,char in enumerate(shortstr):
for other in strs:
if other[i]!=char:
return shortstr[:i]
return shortstr

14. Longest Common Prefix(暴力循环)的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 14. Longest Common Prefix【leetcode】

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  3. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  4. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  5. [LeetCode]14. Longest Common Prefix最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  6. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  7. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. 14. Longest Common Prefix 最长的公共字符串开头

    [抄题]: Write a function to find the longest common prefix string amongst an array of strings. 在 " ...

  9. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

随机推荐

  1. 内部排序->插入排序->其它插入排序->2-路插入排序

    文字描述 在折半插入排序的基础上进行改进, 另设一个和待排序序列L相同的数组D, 首先将L[1]赋值给D[0], 数组D中数据是已经排好序的, first指向最小值下标,final指向最大值下标.初始 ...

  2. Servlet (三) 文件下载(只支持英文文件名)

    package cn.sasa.serv; import java.io.FileInputStream; import java.io.IOException; import java.io.Inp ...

  3. hotplug 热拔插机制框架

    框架入口源文件: mdev.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6     硬件平台:JZ2440 以下是驱动框架:

  4. es调用脚本

    1.内部脚本("script" : "ctx._source" 是内部定义好的获取_source数据的方式,不用改变)POST /index/type/id/_ ...

  5. Innodb semi-consistent 简介

    A type of read operation used for UPDATE statements, that is a combination of read committed and con ...

  6. opencv手工编译

    opencv手工编译方法1.下载cmake gui2.在where is the source code路径下配置opencv根目录,在where to build the binaries路径下配置 ...

  7. java 并发包runnable 与 callable

    1.runnable 与 callable区别 2.避免callable执行完任务,获取返回结果时,阻塞其他子线程 下面固定线程池,设置4个,表明同时只有4个线程在执行任务,当某个线程执行完一个任务, ...

  8. (1.7)mysql profiles分析

    mysql profiles分析 作用:记录会话查询SQL所用时间 1.开启 2.使用 [2.1]先使用一个查询 [2.2]然后再运行 show profiles; [2.3]查看执行过程中每个状态和 ...

  9. Redis入门到高可用(二)—— Redis启动及使用

    1. 三种启动方式 ♦️  最简启动 ./redis-server 使用Redis默认配置进行启动; ♦️  动态参数启动 * redis-server --port 6380  更改端口为6380并 ...

  10. Scala枚举--Enumeration

    object Color extends Enumeration(2){ val Red,Green,Blue = Value val Yellow = Value("YELLOW" ...