1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第十四题

(1)题目
英文:
Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string “”.

Note:

All given inputs are in lowercase letters a-z.

中文:
编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 “”。

说明:

所有输入只包含小写字母 a-z 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-parentheses

(2)解法
① 直接使用现有的方法(耗时:48ms,内存:13.8M)

class Solution:def longestCommonPrefix(self, strs: List[str]) -> str:import osreturn os.path.commonprefix(strs)

② 先确定最小的字符串和相应的长度,然后再在strs中逐一判断每个str的每个字符与最小字符串的匹配情况,如果出现一个不匹配,则停止,并将这个字符串赋值为最小的字符串,然后开始比较下一个字符串。(耗时:36ms,内存:13.8M)

class Solution:def longestCommonPrefix(self, strs: List[str]) -> str:if not strs:return ''min_len = len(strs[0])str_min = strs[0]for str1 in strs:if len(str1)<min_len:min_len = len(str1)str_min = str1for str2 in (strs):i = 0while (i<min_len):if str2[i]!=str_min[i]:str_min = str2[:i]min_len = len(str_min)i+=1return str_min

当然了,也可以使用更简单的方法来找到最小的字符串:

        str_min = min(strs)min_len = len(str_min)

(耗时:40ms,内存:13.7M)

③ 使用min,max方法分别获得,按字母排序最小的和最大的字符串。(耗时:48ms,内存:13.8M)

class Solution:def longestCommonPrefix(self, strs: List[str]) -> str:if not strs:return ""min1 = min(strs)max1 = max(strs)for indx in range(len(min1)):if max1[indx] != min1[indx]:return min1[:indx]return min1

注意:
1.min1[:0]返回的就是’’。

④ 使用zip和sort方法(耗时:32ms,内存:13.7M)

class Solution:def longestCommonPrefix(self, strs: List[str]) -> str:if not strs:return ""strs.sort()res = ""for x, y in zip(strs[0], strs[-1]):if x == y:res += xelse:breakreturn res

注意:
1.strs.sort()是直接修改了strs中的顺序的,而sorted方法需要重新赋值;sort方法可以按字母顺序依次排序字符串。
2.zip如果遇到空的字符与有字符的配对,则直接跳出循环了;zip输出是一个地址,所以必须有for进行输出。

leetcode python3 简单题14. Longest Common Prefix相关推荐

  1. 【easy!】LeetCode 14. Longest Common Prefix

    LeetCode 14. Longest Common Prefix Solution1: 用的暴力遍历,时间复杂度O(n2)O(n2)O(n^2) class Solution { public:s ...

  2. LeetCode:14. Longest Common Prefix

    两年硕士超快的鸭,又要准备秋招啦!0508第一题~ 题目 Write a function to find the longest common prefix string amongst an ar ...

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

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

  4. LeetCode - Easy - 14. Longest Common Prefix

    Topic String Description https://leetcode.com/problems/longest-common-prefix/ Write a function to fi ...

  5. LeetCode: 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 大意就是,写一个函数可以找 ...

  6. Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]

    题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...

  7. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

  8. leetcode python3 简单题53. Maximum Subarray

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...

  9. leetcode python3 简单题225. Implement Stack using Queues

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...

最新文章

  1. matlab disteclud,机器学习实战ByMatlab(3)K-means算法
  2. ASP.NET企业开发框架IsLine FrameWork系列之九--ExceptionProcessProvider异常框架(上)
  3. 提前为小米11让路?小米10高配版官网已缺货
  4. Git不断询问我ssh密钥密码
  5. Educoder Matplotlib和Seaborn 三维图 第一关绘制三维图
  6. 性能优化是数据库应用的核心问题
  7. 汉字取首字母(第三节蓝桥杯决赛)
  8. CleanMyMac偏好设置你知道多少?
  9. MYSQL无法连接,提示10055错误尝试解决
  10. Java指纹识别开源代码SourceAFIS使用入门
  11. oracle 索引问题梳理
  12. 我的Qt作品(11)使用Qt+OpenCV实现一个带旋转角度的NCC灰度模板匹配演示软件
  13. 使用cordova调用相机在相机中添加蒙版
  14. 《必然》二、奔跑吧,所有人都是菜鸡
  15. 网页游戏是如何快速推广,寻找真实玩家。提高充值消费的。这里都有教程
  16. python web py入门(6)-webpy在模板里使用code代码段错误的问题
  17. 高性能永磁交流伺服电机系统控制策略
  18. crh寄存器_STM32的GPIO的寄存器介绍和设置
  19. 谈谈软件开发项目管理的积分制
  20. format转换方法

热门文章

  1. python返回上一条指令必须包括_python基础(10)--模块
  2. python爬虫筛选数据_[python爬虫]使用beautifulsoup库的select方法对网页内容进行筛选...
  3. exc导入mysql phpcms_PHP如何将EXCEL导入MYSQL,急!!!急!!哪位大师能帮帮忙啊,给个详细代码...
  4. 最近公共祖先_LeetCode 236. 二叉树的最近公共祖先
  5. python数据结构之元组(tuple)——超详细
  6. Python之对字符串切片
  7. 无人驾驶——路径规划的学习笔记
  8. 【可行】adb修改手机代理方式
  9. AD在原理图中高亮网络的两种方法,altium designer 20
  10. 【多线程】CAS乐观锁