Longest Common Prefix [LeetCode 14]
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]的更多相关文章
- Longest common prefix | leetcode
Write a function to find the longest common prefix string amongst an array of strings. 思路:要去是寻找字符串ve ...
- Longest Common Prefix leetcode java
题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
随机推荐
- Mac下配置JAVA_HOME
http://blog.csdn.net/shallowgrave/article/details/39367119 闲来无事,装个Hbase玩玩,突然发现Mac下默认安装的JDK7,没有配置JAVA ...
- Think in UML笔记第1章--为什么要UML
1.1 面向过程还是面向对象 面向过程和面向对象都是一种软件技术.例如把面向过程归纳为结构化程序设计.DFD图.ER模型.UC矩阵等,而面向对象则被归纳为继承.封装.多态.复用等具体的技术.事实上,上 ...
- Xcode 的正确打开方式——Debugging(转载)
Xcode 的正确打开方式——Debugging 程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能 ...
- Python多个版本安装!
Python可以同时安装多个版本,目前我安装的是3.6和3.5,在Eclipse中使用3.6:在Visual Studio中使用3.5.如何让哪个版本的Python成为系统默认的解释器呢?通过调整不同 ...
- 第一个APP:IOS做简单运算的计算器
步骤: 1.打开Xcode,单机Creat a new Xcode project 2.左边选择ios下Application,右边选择single view Application 3.填写项目名称 ...
- Nginx 配置指令location 匹配符优先级和安全问题【转】
Nginx配置指令location匹配符优先级和安全问题 使用nginx 很久了,它的性能高,稳定性表现也很好,得到了很多人的认可.特别是它的配置,有点像写程序一样,每行命令结尾一个";&q ...
- C语言 段位
C语言允许在一个结构体中以位为单位来指定其成员所占内存长度,这种以位为单位的成员称为"位段"或"位域" 在结构体中位段的定义格式为: unsignede < ...
- [ActionScript 3.0] AS3 时间日期格式化DateTimeFormatter类的运用
import flash.globalization.DateTimeFormatter; var _timeFormatter:DateTimeFormatter; var _dateFormatt ...
- [ActionScript 3.0] AS3调用百度地图API
package { import baidu.map.basetype.LngLat; import baidu.map.basetype.Size; import baidu.map.config. ...
- photoshop CS 调整选择区域的大小
网上看到说:矩形选框不能直接调整大小,如果你不想重新画一个可以利用转换路径,然后再调整.这是不对的,矩形选框是可以调整大小的,使用"变换选区"即可. 对应步骤截图如下: 1.画 ...