leetcode笔记--Longest Common Prefix
来源:互联网 发布:maxonor创意公元软件 编辑:程序博客网 时间:2024/06/18 03:32
题目:难度(Easy)
Write a function to find the longest common prefix string amongst an array of strings.
Tags:String
分析:找字符串数组中字符串们的最长公共前缀
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if len(strs) == 0: return "" #为smallLength给上第一个字符串长度的初值 smallLength = len(strs[0]) for s in strs: if len(s) < smallLength: smallLength = len(s) #此时smallLength为最小长度,i指示第i个字符,j指示strs里的第j个字符串 endFor = False i = 0 while i < smallLength: #ch为第1个字符串的第i个字符 ch = strs[0][i] j = 1 while j < len(strs): if strs[j][i] != ch: endFor = True break j += 1 if endFor is True: break i += 1 return strs[0][:i]
0 0
- leetcode笔记:Longest Common Prefix
- leetcode笔记--Longest Common Prefix
- LeetCode笔记:14. Longest Common Prefix
- LeetCode小白菜笔记[5]:Longest Common Prefix
- LeetCode: Longest Common Prefix
- LeetCode Longest Common Prefix
- LeetCode : Longest Common Prefix
- [Leetcode] Longest Common Prefix
- [LeetCode] Longest Common Prefix
- [LeetCode]Longest Common Prefix
- LeetCode-Longest Common Prefix
- [LeetCode] Longest Common Prefix
- LeetCode - Longest Common Prefix
- leetcode Longest Common Prefix
- LeetCode | Longest Common Prefix
- Leetcode: Longest Common Prefix
- LeetCode: Longest Common Prefix
- 【LeetCode】Longest Common Prefix
- 细说网关
- 新年新气象
- 学一点 mysql 双机异地热备份----快速理解mysql主从,主主备份原理及实践
- 单例模式的七种写法
- jQuery-UI 拖动Div交换位置
- leetcode笔记--Longest Common Prefix
- 解析Xml的理解
- 数据结构课程设计报告-职工信息管理系统
- 如何使用 Java8 实现观察者模式?(上)
- JavaScript(三)-- DOM编程
- 华为P8解锁问题:waiting for device解决方法(亲测成功)
- oracle 恢复误删除数据
- ICPCCamp 2016 Day 3 - Grand Prix of China(Random Arithmetic-dp)
- Java开发环境搭建