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 ...
随机推荐
- 利用cookies获取登录后的网页
众所周知,HTTP连接是无状态的,那么问题来了,怎么记录用户的登录信息呢?通常的做法是用户第一次发送HTTP请求时,在HTTP Server端生成一个SessionID,SessionID会对应每个会 ...
- Python中for\while的用法
代码示例 board = [] for i in range(5): board.append(i) print board board = [] i = 0 while i < 5: boar ...
- javaScript中小数取整,四种方法的比较
1.parseInt:只取整数位例如:parseInt(3.7) 取整结果为:3parseInt(-1.1) 取整结果为:-1 2.Math.floor :向下去整,取大的整数例如:Math.floo ...
- mysql create routine 权限的一些说明
1.如果用户有create routine 权限那么他就可以创建procedure | function . 2.如果用户创建了procedure | function 那么mysql 会自动赋予它对 ...
- mysql----show slave status \G 说明
show slave status \G 可以用来查看mysql 的复制状态,有些列名所表达的意思不太明确,现整理如下: 1. Slave_IO_State:ID线程的状态,如果master 的所有变 ...
- Ext4 MVC CRUD操作
项目目录结构如下: (1)index.html <!DOCTYPE html> <html> <head> <title>用户管理</title& ...
- Shell使用
http://www.cnblogs.com/hbt19860104/archive/2012/08/14/2638952.html http://blog.csdn.net/tttyd/articl ...
- 开源C/C++网络库比较
在开源的C/C++网络库中, 常用的就那么几个, 在业界知名度最高的, 应该是ACE了, 不过是个重量级的大家伙, 轻量级的有libevent, libev, 还有 Boost的ASIO. ACE是一 ...
- 自制简单表单验证relative与absolute定位
html结构,用到了label与span <label class="relative"><input type="text" name=&q ...
- error LNK2019
error LNK2019: 无法解析的外部符号 "public: virtual __thiscall Fruit::~Fruit(void)" (??1Fruit@@UAE@X ...