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 ...
随机推荐
- django获取某一个字段的列表,values/values_list/flat
class Building(models.Model): corporation = models.ForeignKey('company.Corporation', verbose_name=u' ...
- python Post方式发起http请求 使用百度接口地理编码
import os import httplib import json import urllib baiduapi="api.map.baidu.com:80" src=&qu ...
- Super Jumping! Jumping! Jumping!(hdu 1087 LIS变形)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- LeetCode_Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- VCL线程的同步方法 Synchronize(用消息来同步)
看本文时,可以同时参考:Delphi中线程类 TThread实现多线程编程(事件.临界区.Synchronize.WaitFor……) 先说一下RTL和VCL RTL(Run-Time library ...
- invesments 第三章 上
1. How firms issue securities: 公司如何发行股票 A. primary market: 新的股票,债券和其他的证券第一次发行的market B. ...
- Remove Element 解答
Question Given an array and a value, remove all instances of that value in place and return the new ...
- Safari HTML5 Canvas Guide: Creating Charts and Graphs
Safari HTML5 Canvas Guide: Creating Charts and Graphs Bar graphs are similar to data plots, but each ...
- 【转】C++容器类
C++容器类 C++中的容器类包括“顺序存储结构”和“关联存储结构”,前者包括vector,list,deque等:后者包括set,map,multiset,multimap等. 若需要存储的元素数在 ...
- 【转】android camera(二):摄像头工作原理、s5PV310 摄像头接口(CAMIF)
关键词:android camera CMM 模组 camera参数 CAMIF平台信息:内核:linux系统:android 平台:S5PV310(samsung exynos 4210) 作者 ...