Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix
题目
给予一个列表,元素为字符串,写一个程序找出最长公共前缀
解题思路
先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下一个元素继续比较,以此类推
class Solution:
# @param {string[]} strs
# @return {string}
def longestCommonPrefix(self, strs):
if not strs:
return ''
if len(strs) == 1:
return strs[0]
i = 0
while i+1 < len(strs):
temp = ''
for j in range(min(len(strs[i]), len(strs[i+1]))):
if strs[i][0] != strs[i+1][0]:
return ''
if strs[i][j] == strs[i+1][j]:
temp += strs[i][j]
i += 1
strs[i] = temp
return strs[len(strs)-1]
Leetcode算法刷题:第14题 Longest Common Prefix的更多相关文章
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 【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 OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- LeetCode 14: Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【LeetCode】LeetCode——第14题:Longest Common Prefix
14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...
随机推荐
- php中12个魔术方法
本文列举了php面向对象当中12个魔术方法,并对此进行一一详细介绍,希望对新手有所帮助. 1.构造方法: __construct() 参数:自定义 触发时机:new的一瞬间自动调用 作用:初始化成员属 ...
- Effective Java单元测试JUnit - 就是爱Java
实作了RoleImpl class,现在要开始单元测试了,或许你会觉得奇怪,才刚做好一个class而已,它并没有商业规则,只有getter/setter与clone(),那是要测试什么呢?没错,传统上 ...
- 八大排序c++可运行精简版,一目了然
#include <iostream> using namespace std; // 插入排序开始===================== void insert_sort(int a ...
- button元素兼容问题浅析
缺省type属性值 <button>提交</button> button元素的type属性值有submit.button可选,在上面这种没有明确指出type值的情况下,浏览器的 ...
- Unique Paths II 解答
Question Follow up for "Unique Paths": Now consider if some obstacles are added to the gri ...
- HDU 3634 City Planning (离散化)
City Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 【转】DynDNS使用随笔
暂且小结一下: 1.下载编译客户端代码并交叉编译 首先,按照网上提示的步骤,在www.dyndns.com注册了帐号,并申请了域名,绑定了IP; 然后,在站点中找到客户端源码,其中ddclient是p ...
- 【转】android camera(一):camera模组CMM介绍
关键词:android camera CMM 模组 camera参数平台信息:内核:linux系统:android 平台:S5PV310(samsung exynos 4210) 作者:xubin ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式
原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least ...