1- 问题描述

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


2- 思路分析

  将数组内每个字串转换为List,每次批量取出各列表对应元素存入新列表,对新列表使用set去重。若set长度为1,说明元素一样,算入前缀。


3- Python实现

 class Solution:
# @param {string[]} strs
# @return {string}
def longestCommonPrefix(self, strs):
if not strs: return ''
l = len(strs)
if l == 1: return strs[0]
# 方法1 求出最短字串长度再遍历
minlen = min(map(lambda x: len(x), strs))
res = []
for i in range(minlen):
tmp = []
for j in range(l):
tmp.append(strs[j][i]) # 各字串i位置字符存入列表
if len(set(tmp)) != 1: break # 若set后长度不等于1,说明存在多种字符
res.append(tmp[0])
return ''.join(res) # 拼接出前缀 '''
# 方法2 不求最短字串长度,若某字串无法取字符,抛出异常
res = []
for i in range(len(strs[0])):
tmp = []
for j in range(l):
try:
tmp.append(strs[j][i])
except:
break # 取不出字符,跳出循环
if len(tmp) != l: break # 若是异常结束循环,已经有部分字符存入tmp,判断是否每个字串都取出字符
if len(set(tmp)) != 1: break
res.append(tmp[0])
return ''.join(res)
'''

Longest Common Prefix [LeetCode 14]的更多相关文章

  1. Longest common prefix | leetcode

    Write a function to find the longest common prefix string amongst an array of strings. 思路:要去是寻找字符串ve ...

  2. Longest Common Prefix leetcode java

    题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...

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

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

  4. [LeetCode] 14. Longest Common Prefix 最长共同前缀

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

  5. LeetCode 14. 最长公共前缀(Longest Common Prefix)

    14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...

  6. 14. Longest Common Prefix【leetcode】

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

  7. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  8. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

随机推荐

  1. UCOS-信号量(学习笔记)

    当事件控制块类型为OS_Event_Type_SEM类型时就是信号量,包含2个内容:信号量计数器OSEventcnt和等待列表OSEventTbl[]. 一创建信号量:OSSemCreat(int16 ...

  2. border-radius 样式表CSS3圆角属性

    border-radius 是CSS3圆角属性,用来实现DIV层的4个边框画成圆角. 一.语法: border-radius : none | <length>{1,4} [/ <l ...

  3. 配置FileZilla Ftp服务器

    FileZilla是我比较喜欢用的一款FTP服务端和客户端,主要使用在Windows下,她是一款开源的FTP软件,虽然在某些功能上比不上收费软件Ser-u,但她也是一款非常好用的软件,这里主要说一下这 ...

  4. Java中的匿名类

    我们知道接口一般用于定义一种规范或操作协议,然后子类实现接口的所有功能.如下面的简单代码: 定义IMessage接口 package org.lyk.entities; public interfac ...

  5. shell 统计 awk

    time awk '{a[$1]++}END{for(i in a){printf("%d\t%s\n",a[i],i)}}' access.log | sort -nr | he ...

  6. angularJs中的隐藏和显示

    <!DOCTYPE html> <html ng-app="a2_12"> <head> <meta charset="utf- ...

  7. ubuntu 14.10 安装 zabbix

    在ubuntu 14.10 上部署 zabbix 2.x 基本软件包安装 既然是ubuntu系统,当然要用好apt-get神器. 参考教程 URL:http://blog.csdn.net/cloud ...

  8. [POJ 1787]Charlie's Change (动态规划)

    题目链接:http://poj.org/problem?id=1787 题意:有4种货币分别是1元,5元,10元,20元.现在告诉你这四种货币分别有多少个,问你正好凑出P元钱最多可以用多少货币.每种货 ...

  9. 【转】android的一些开源项目

    自己一直很喜欢Android开发,就如博客副标题一样,我想做个好的App. 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者,而且我会不定期的更新这篇文章. ...

  10. (转).net Application.DoEvents()的作用

    原文地址:http://blog.csdn.net/weinierbian/article/details/6231589 Application.DoEvents()的作用:处理所有的当前在消息队列 ...