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 ...
随机推荐
- JAVA 构造代码块
class G{ G(){ System.out.println("我是无参构造方法"); } G(String name){ System.out.println("我 ...
- map遍历测试结果
结论:一般情况下推荐使用enterSet的for循环(即以下的方法2),如果只是取key值可以使用keySet性能会更好. 因为keySet只取key,enterSet即取了key又取了value. ...
- Python深入03 对象的属性
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python一切皆对象(object),每个对象都可能有多个属性(attribut ...
- 在存储过程中执行3种oracle循环语句
create or replace procedure pr_zhaozhenlong_loop /* 名称:在存储过程中执行3种循环语句 功能:利用循环给表中插入数据 调用: begin -- Ca ...
- (medium)LeetCode 229.Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- java cmd 命令
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是执行完d ...
- JavaScript对象的创建总结
方式 缺点 优点 基于已有对象扩充属性和方法 不可重用,没有约束 无 工厂方法 检测不出是什么的实例 简单封装,可以传参 构造方法 每创建一个对象就有开辟存放方法的空间 能通过instanceof检测 ...
- Eclipse Gtk+
From: http://hi.baidu.com/spmno/item/9425018707f295dfd1f8cdbe 1. Project->Properties,然后选 C/C++ Bu ...
- Oracle中MD5+Base64加密实现
JAVA实现: public static String getMD5(String str) throws Exception { MessageDigest md5 = MessageDige ...
- C语言sizeof陷阱
执行以下程序,查看输出: #include <stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int ...