leetcood学习笔记-14*-最长公共前缀
笔记:
python if not
判断是否为None的情况
if not x
if x is None
if not x is None
if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。
使用if not x这种写法的前提是:必须清楚x等于None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行
链接:https://www.cnblogs.com/chenya/p/4218761.html
题目描述:
第一次提交:
class Solution:
def longestCommonPrefix(self, strs ):
'''
:param strs: List[str]
:return: -> str:
'''
commen=""
if not strs:
return commen
for i in range(len(strs[0])):
temp = ""
for s in strs:
if len(s)>i:
if temp=="":
temp=s[i]
#print(temp)
elif s[i]!=temp:
return commen
else:
return commen
commen+=temp return commen
方法二:利用python的max()和min(),在Python里字符串是可以比较的,按照ascII值排,举例abb, aba,abac,最大为abb,最小为aba。所以只需要比较最大最小的公共前缀就是整个数组的公共前缀
class Solution:
def longestCommonPrefix(self, strs ):
'''
:param strs: List[str]
:return: -> str:
'''
if not strs:
return "" s1=min(strs)
s2=max(strs)
for i,x in enumerate(s1):
if x!=s2[i]:
return s2[:i]
return s1
方法三:利用python的zip函数,把str看成list然后把输入看成二维数组,左对齐纵向压缩,然后把每项利用集合去重,之后遍历list中找到元素长度大于1之前的就是公共前缀
class Solution:
def longestCommonPrefix(self, strs ):
'''
:param strs: List[str]
:return: -> str:
'''
if not strs:
return ""
ss= list(map(set,zip(*strs)))
res=""
for i,x in enumerate(ss):
x=list(x)
if len(x)>1:
break
res=res+x[0]
return res
笔记:
Python zip() 函数
描述
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中为了减少内存,zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。
链接:http://www.runoob.com/python/python-func-zip.html
其他方法:分治法,二分法 见官网题解
leetcood学习笔记-14*-最长公共前缀的更多相关文章
- 【Leetcode】【简单】【14最长公共前缀】【JavaScript】
题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- Java实现 LeetCode 14 最长公共前缀
14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...
- [LeeCode]14. 最长公共前缀
题目链接:https://leetcode-cn.com/problems/longest-common-prefix/ 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀 ...
- python(leetcode)-14最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- 【LeetCode】14. 最长公共前缀
题目 编写一个函数来查找字符串数组中的最长公共前缀.如果不存在公共前缀,返回空字符串 "". 示例 1:输入: ["flower","flow&quo ...
- 力扣(LeetCode) 14. 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- Leetcode题库——14.最长公共前缀
@author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...
- leetcode 14 最长公共前缀
描述: 给个字符串vector,求最长公共前缀. 解决: 直接取第一个字符串作为最长公共前缀,将其每个字符遍历过一次.设最长字符实际为k,共n个元素,则复杂度O(nk) string longestC ...
随机推荐
- 浅谈JavaScript原型图与内存模型
js原型详解 1.内存模型: 1.原型是js中非常特殊一个对象,当一个函数(Person)创建之后,会随之就产生一个原型对象 2. 当通过这个函数的构造函数创建了一个具体的对象(p1)之后,在这个具体 ...
- python--MySql 表记录的操作
表记录的增删改查 ---插入表记录 全列插入:insert into 表名 values(...) 缺省插入:insert into 表名(列1,...) values(值1,...) -- 插入一条 ...
- UVA 10529 - Dumb Bones (概率dp)
题目描述 You are trying to set up a straight line of dominos, standing on end, to be pushed over later f ...
- 66、saleforce 的 approval process
public class TestApproval { public void submitAndProcessApprovalRequest() { // Insert an account Lin ...
- Jmeter 5.1命令行执行bat文件
一.编写run_jmeter,bat @echo off::设置参数::参考命令:jmeter -n -t d:\123.jmx -l result.jtl -e -o d:\report\repor ...
- 运维 07 Linux系统基础优化及常用命令
Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令 ...
- Python运维-获取当前操作系统的各种信息
#通过Python的psutil模块,获取当前系统的各种信息(比如内存,cpu,磁盘,登录用户等),并将信息进行备份 # coding=utf-8 # 获取系统基本信息 import sys impo ...
- add characteristic to color
Problem: add a new Char. name D_COI6 that the description is Injected coloration #7 (COI6) in the D_ ...
- EntityFramework 知识点与sql优化汇总
一.EntityFramework modelBuilder.Entity<Domain.UseOilPlanDetail>().HasRequired(x => x.MainOil ...
- 消费kafka的消息,并将其SparkStreaming结果保存到mysql
将数据保存到mysql,需要用到jdbc.为了提高保存速度,我写了一个连接池 1.保存到mysql的代码 package test05 import org.apache.log4j.{Level, ...