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. 无状态服务(stateless service)

    一.定义 无状态服务(stateless service)对单次请求的处理,不依赖其他请求,也就是说,处理一次请求所需的全部信息,要么都包含在这个请求里,要么可以从外部获取到(比如说数据库),服务器本 ...

  2. 破解ckfinder2.3 去除版本号和标题提示

    1.找到ckfinder.js 2.找到这样的字符串 <h4 class='message_content'></h4> 3.改成这样 <h4 style='displa ...

  3. python学习笔记——异常

    转自 http://www.cnblogs.com/rubylouvre/archive/2011/06/22/2086644.html Python内建异常体系结构 BaseException +- ...

  4. Django下载中文名文件:

    Django下载中文名文件: from django.utils.http import urlquote from django.http import HttpResponse content = ...

  5. 安装LINUX X86-64的10201出现链接ins_ctx.mk错误-转自yingtingkun

    详细错误信息为: Error in invoking target ‘install’ of makefile ‘/opt/oracle/product/10.2/ctx/lib/ins_ctx.mk ...

  6. [SQL]不知道1

    表结构,companyid是公司名,productid是产品号,说明每个公司生产多少种产品. companyid productid A A B B B C D D D 要求:取出所有生产的产品包含公 ...

  7. window上Python环境的搭建

    python下载地址:https://www.python.org/ 下载安装 安装完成后配置环境变量,在我的电脑右键属性点高级设置 双击 环境变量 里面第二框找到 path双击     在pytho ...

  8. @valid表单验证demo

    springMVC 表单验证demo  视图层使用的是jsp

  9. sqoop安装

    环境:Hadoop 2.3.0 sqoop 1.4.5 1.下载并解压sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz (解压完,名字会很长,可以根据需要自己修改下 ...

  10. 动手学servlet(四) cookie和session

    Cookie   cookie是保存在客户端的一个“键值对”,用来存储用户的一些信息 cookie的应用: -在电子商务会话中标识用户 -对网站进行定制,比如你经常浏览哪些内容,就展示哪些页面给你 - ...