当前位置:网站首页>leetcode答题笔记(一)
leetcode答题笔记(一)
2022-04-21 14:27:00 【夜雨_小学徒】
- 背景:最近在leetcode做题,特在此写博客记录一下所犯的错误和改正。
- 题目:最长公共前缀:
- 描述:编写一个函数来查找字符串数组中的最长公共前缀;如果不存在公共前缀,返回空字符串
""。 - 示例一:
理解:从例子中可以看出,公共前缀是字符串从左至右开始,共有的字符,可以使用切片进行操作。输入: ["flower","flow","flight"] 输出: "fl" - 示例二
输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀。理解:只要第一个和第二个之间没有共有的前缀,就可以返回“”,因此可以编写一个子函数用于判断两个字符串之间是否有公共前缀。
-
代码
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: # 当strs=[]或者strs=['']或者strs=['',''] if len(strs) == 0 or strs.count("") >= 1: return "" # 当strs只有一个非空字符串时,如['a'] if "" not in strs and len(strs) == 1: return strs[0] # 求两个字符串之间的公共前缀 def public_str(a, b): n = len(a) while n: if b.startswith(a[:n]): return a[:n] n = n - 1 i = strs[0] str_lists = [] for j in strs[1:len(strs)]: str_num = public_str(i, j) # 当两个字符串相比没有公共前缀时,函数public_str会返回None,而对于None类型,min无法判断 if str_num == None: return '' str_lists.append(str_num) return min(str_lists) -
总结:
# 列表为[""]时,长度是1,而不是0
len([""])
1
# 切片操作是左闭右开区间,返回值的数量是区间右值-区间左值
'c'[0:0]
''
# min要有比较的东西,None就不能用min
min(['',''])
''
# startswith中间有’s'
版权声明
本文为[夜雨_小学徒]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_37992521/article/details/100008690
边栏推荐
- 你会“卖”SQL?
- 2023年北京外国语大学汉语国际教育考研上岸前辈备考经验指导
- In depth analysis of TCP three handshakes, the interviewer applauded
- 2023年东北师范大学应用心理考研上岸前辈备考经验指导
- Brutally crack the latest JVM interview question of meituan: unlimited execution
- Insect binary tree OJ
- 虫子 时空复杂度
- 使用枚举做的红绿灯,有界面
- Experience and guidance of seniors preparing for the postgraduate entrance examination of Applied Psychology in Northeast Normal University in 2023
- Why is it necessary to override the hashcode () method when overriding the equals () method
猜你喜欢

Implementation of basic operation of binary tree

word输入公式快捷键

Sequential list -- linked list implementation

数据仓库架构演变和建设思路

Use go language to complete the student information management system through restful API

How to insert a file into excel? What is the difference between excel insert object and attachment? (the inserted object can directly display the content, but I didn't display it?)

Ros2 learning notes (IX) -- Summary of common instructions for ros2 command line operation (II)

无人驾驶虚拟仿真(十三)--图像处理之交通标志牌识别1

传奇服务器架设教程,传奇GM权限命令设置教程

toString的使用与包装类的使用
随机推荐
PCL测试程序出现LNK2001-无法解析的外部符号
word输入公式快捷键
虫子 文件操作
虫子 双链表
Distributed database -- Plan hint of SQL optimization
顺序表--链表实现
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
Personal summary of linked list examples
浅谈NFT的注意力经济
Experience and guidance of seniors preparing for the postgraduate entrance examination of resources and environment of Dalian University of technology in 2023
UE4锁定Camera画面
SQL Server 批处理变量定义和赋值
Why does MySQL index use B + tree instead of jump table?
A quietly rising domestic software
WTL 自绘控件库 (CQsTabCtrl)
shell sed 和 gawk 编辑器使用
Use go language to complete the student information management system through restful API
快速排序几种实现方法及其优化
如何在代码层面提供CPU分支预测效率
ROS2学习笔记(九)-- ROS2命令行操作常用指令总结(二)