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 ...
随机推荐
- C# 利用反射根据类名创建类的实例对象
“反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 1.假设你要反射一个 DLL 中的类,并且没有引用它(即未知的类型): ...
- JDBC连接各种数据库的地址名称
oracle driverClass:oracle.jdbc.driver.OracleDriver url:jdbc:oracle:thin:@127.0.0.1:1521:dbname ...
- 85 megacli-查看raid信息
文章本身我不做过多修改了,在这里我就把自己在安装时候碰到的难点跟大家提下.1.何处下载?首先,根据文章中的路径已经下载不到相应的文件了,在此我们就自己到http://www.lsi.com的网站上去搜 ...
- 利用python合并两个文件
1格式如下 在做利用zabbix的api来批量添加主机的时候,需要处理ip和hostname,在借用别人写的py程序的基础上,自己有改装了以下脚本,为自己使用.需要时ip和hostname为一个统一格 ...
- spring-poi-excle往单元格写入图片
HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 在POI中有HSSFPat ...
- QQ个人文件夹中的文件被占用,解决办法
我的情况是记住密码的账号不可以登录,不记住密码的账号确可以登录,突然就这样,我也很郁闷. 找到路径C:\Users\Public\Documents\Tencent\QQ下的UserDataInfo. ...
- 教你一招:解决Win10 win7 删除文件或文件夹时提示“找不到该项目”
问题很怪异,解决的办法却很简单. 首先,分析问题 使用不可显示ASCII字符或采用UNICODE字符方法创建的文件或文件夹: 名称中含有..等特殊符号文件或文件夹名称不符合Windows命名规范或建立 ...
- Ubuntu(基于Ubuntu)中常用的apt和dpkt命令
apt-get sudo apt-get install package 安装包 sudo apt-get -f install 修复安装”-f = ——fix-missing” sudo a ...
- 数据结构图文解析之:直接插入排序及其优化(二分插入排序)解析及C++实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- reGeorg v1.0内网流量转发
reGeorg v1.0 git Usage $ reGeorgSocksProxy.py [-h] [-l] [-p] [-r] -u [-v] Socks server for reGeorg H ...