Longest Common Prefix [LeetCode 14]
1- 问题描述
Write a function to find the longest common prefix string amongst an array of strings.
2- 思路分析
将数组内每个字串转换为List,每次批量取出各列表对应元素存入新列表,对新列表使用set去重。若set长度为1,说明元素一样,算入前缀。
3- Python实现
class Solution:
# @param {string[]} strs
# @return {string}
def longestCommonPrefix(self, strs):
if not strs: return ''
l = len(strs)
if l == 1: return strs[0]
# 方法1 求出最短字串长度再遍历
minlen = min(map(lambda x: len(x), strs))
res = []
for i in range(minlen):
tmp = []
for j in range(l):
tmp.append(strs[j][i]) # 各字串i位置字符存入列表
if len(set(tmp)) != 1: break # 若set后长度不等于1,说明存在多种字符
res.append(tmp[0])
return ''.join(res) # 拼接出前缀 '''
# 方法2 不求最短字串长度,若某字串无法取字符,抛出异常
res = []
for i in range(len(strs[0])):
tmp = []
for j in range(l):
try:
tmp.append(strs[j][i])
except:
break # 取不出字符,跳出循环
if len(tmp) != l: break # 若是异常结束循环,已经有部分字符存入tmp,判断是否每个字串都取出字符
if len(set(tmp)) != 1: break
res.append(tmp[0])
return ''.join(res)
'''
Longest Common Prefix [LeetCode 14]的更多相关文章
- Longest common prefix | leetcode
Write a function to find the longest common prefix string amongst an array of strings. 思路:要去是寻找字符串ve ...
- Longest Common Prefix leetcode java
题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- [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)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- 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
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
随机推荐
- Map的3种遍历[轉]
Map<String, String> map = new HashMap<String, String>(); map.put("A", "AA ...
- protobuf使用说明
1..proto文件为要生成.java文件的模板文件,其中包含名称空间.文件名等信息2.cmd中进入当前目录D:\JAVA\protoc-2.5.0-win323.运行 protoc.exe --ja ...
- android之location02
package com.example.mars_3300_location02; import java.net.ContentHandler; import java.util.List; imp ...
- 关于position的疑惑
我到现在也不是很清楚position的效果会是什么,或者是什么情况下需要指明为relatieve什么情况下又需要absolute呢? <div style="position:rela ...
- SQL 去特殊字符
)) ) as begin declare @i int while patindex('%[^%@+*,=../_ <>''" ^0-9 ^a-Z ^''- ^吖-座]%' , ...
- ms sqlserver 系列之如何查看数据链接数
[转]如何查看SQL SERVER数据库当前连接数 1.通过管理工具开始->管理工具->性能(或者是运行里面输入mmc)然后通过添加计数器添加 SQL 的常用统计然后在下面列出的项目里面选 ...
- startService()和bindService()区别
1. 生命周期:startService()方式启动,Service是通过接受Intent并且会经历onCreate()和onStart().当用户在发出意图使之销毁时会经历onDestroy(),而 ...
- Temporary Segments: What Happens When a Sort Occurs (文档 ID 102339.1)
APPLIES TO: Oracle Database - Enterprise Edition - Version 8.1.7.4 to 11.2.0.1 [Release 8.1.7 to 11. ...
- OC基础(23)
NSArray基本概念 NSArray 遍历 NSArray排序 NSArray文件读写 NSArray 与字符串 *:first-child { margin-top: 0 !important; ...
- 将 node.js 的数据保存到 mongo 数据库中
Mongo 数据库 安装 首先到 Mongo 的官方网站下载安装程序:http://www.mongodb.org/,我下载的文件名为:mongodb-win32-x86_64-2008plus-2. ...