14. Longest Common Prefix
题目:
Write a function to find the longest common prefix string amongst an array of strings.
Subscribe to see which companies asked this question
代码:
上周出差北京一周,都没有做题,这两天继续开始,先从简单的入手。
这个题目一开始以为是找出字符串数组中最长的那个,那不很简单嘛,很快写完了,发现不对。
题目表达不是很清楚,或者是我英语不行,查了下,原来是找出字符串数组中最长的共有前缀。
于是乎,开始凑答案啦!继续用python,写起来比较简单:)
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
result = ""
#输入[],返回""
if len(strs) == 0:
return result
#flag用来判断字符串前缀是否存在于所有的字符串中
flag=True
#以数组中第一个字符串为例,逐步取它的前一位、两位、三位...作为前缀,去数组中所有字符串中判断
#直至失败,说明该字符串不符合要求,于是退回一位到符合要求的字符串前缀,返回结果
j=1
while j in range(1,len(strs[0])+1) and flag:
result=strs[0][0:j]
for i in strs:
if result not in i[0:j]:
flag = False
result=result[:-1]
break
j += 1
return result
发现结果居然效率还不错:
14. Longest Common Prefix的更多相关文章
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- leetCode练题——14. Longest Common Prefix
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- 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] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
随机推荐
- Chrome开发者工具详解
http://www.cnblogs.com/LibraThinker/p/5948448.html http://www.cnblogs.com/LibraThinker/p/5981346.htm ...
- 图片轮播图插件的使用 unslider!!!
1.百度图片轮播图unslider,第一个就会出现jquery unslider轮播图,点击进去,下载网站提供的文件,解压,内部有我们需要使用的各种js,图片等. 2.在自己的eclipse或者int ...
- iOS开发知识点总结
main文件做了这几件事: 1. 创建当前的应用程序 2. 根据4个参数的最后为应用程序设置代理类(默认情况下是AppDelegate) 3. 将appDelegate 和 应用程序 建立关联(指定代 ...
- datatable刷新表头
使用jQuery的datatable生成表格数据,当需要改变表头时,调用Table.fnDestroy();再重新创建表头 var oTable = null; function initSettin ...
- Ubuntu 14.04.4官方默认更新源sources.list
Ubuntu 14.04.4官方默认更新源sources.list # deb cdrom:[Ubuntu LTS _Trusty Tahr_ - Release amd64 (20160217.1) ...
- Mvc视图引擎、寻址规则
目前MVC中用的较多的视图引擎应该是WebFormViewEngine和RazorViewEngine了. 一个Request请求首先会进入Routing进行判断,对于错误的url是不能被路由匹配到的 ...
- cogs 577 蝗灾 CDQ分治
第一道CDQ,抄了下helenkeller的代码,感觉和归并排序差不多... 因为左半边的修改肯定在右半边的询问之前,所以就不用管时间的限制了,可以直接x轴排序树状数组处理y轴... #include ...
- BZOJ3489: A simple rmq problem
设$i$的前驱为$p_i$,后继为$q_i$,把询问看成点$(L,R)$,有贡献的$i$满足$L\in(p_i,i]$且$R\in[i,q_i)$,询问的就是覆盖这个点的矩形的最大值.那么可以用可持久 ...
- Linux系统概述
1.Linux是一套免费使用 和自 由传播的类Unix操作系统. 这个系统是由世界各地的成千上万的程序员 设计和实现的.其目 的是建立不受任何商品化软件的版权制约的. 全世界都能自 由使用的Unix兼 ...
- 要学Java,怎么高效地学习,怎么规划
要学Java,怎么高效地学习,怎么规划? 题主是一个个例,99%的人(包括我自己)都没有题主这样的经历,也很难提出具有很强参考性的java学习建议.我倒是之前面试过一个跟题主有点类似的人,拿出来分 ...